mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-03-29 00:18:20 +08:00
Improved redux-actions type inferencing of createAction and handleAction
This commit is contained in:
71
redux-actions/index.d.ts
vendored
71
redux-actions/index.d.ts
vendored
@@ -9,80 +9,57 @@ declare namespace ReduxActions {
|
||||
// FSA-compliant action.
|
||||
// See: https://github.com/acdlite/flux-standard-action
|
||||
interface BaseAction {
|
||||
type: string
|
||||
type: string;
|
||||
}
|
||||
|
||||
export interface Action<Payload> extends BaseAction {
|
||||
payload?: Payload
|
||||
error?: boolean
|
||||
meta?: any
|
||||
payload?: Payload;
|
||||
error?: boolean;
|
||||
}
|
||||
|
||||
export interface ActionMeta<Payload, Meta> extends Action<Payload> {
|
||||
meta: Meta
|
||||
meta: Meta;
|
||||
}
|
||||
|
||||
type PayloadCreator<Input, Payload> = (...args: Input[]) => Payload;
|
||||
interface ReducerMap<State, Payload> {
|
||||
[actionType: string]: Reducer<State, Payload>;
|
||||
}
|
||||
|
||||
type MetaCreator<Input, Payload> = (...args: Input[]) => Payload;
|
||||
interface Reducer<State, Payload> {
|
||||
(state: State, action: Action<Payload>): State;
|
||||
}
|
||||
|
||||
type Reducer<State, Payload> = (state: State, action: Action<Payload>) => State;
|
||||
interface ReducerMeta<State, Payload, Meta> extends Reducer<State, Payload> {
|
||||
(state: State, action: ActionMeta<Payload, Meta>): State;
|
||||
}
|
||||
|
||||
type ReducerMeta<State, Payload, Meta> = (state: State, action: ActionMeta<Payload, Meta>) => State;
|
||||
|
||||
type ReducerMap<State, Payload> = {
|
||||
[actionType: string]: Reducer<State, Payload>
|
||||
};
|
||||
|
||||
export function createAction(
|
||||
export function createAction<Payload>(
|
||||
actionType: string,
|
||||
payloadCreator?: PayloadCreator<any, any>,
|
||||
metaCreator?: MetaCreator<any, any>
|
||||
): (...args: any[]) => Action<any>;
|
||||
payloadCreator?: (...args: any[]) => Payload,
|
||||
): (...args: any[]) => Action<Payload>;
|
||||
|
||||
export function createAction<InputAndPayload>(
|
||||
export function createAction<Payload, Meta>(
|
||||
actionType: string,
|
||||
payloadCreator?: PayloadCreator<InputAndPayload, InputAndPayload>
|
||||
): (...args: InputAndPayload[]) => Action<InputAndPayload>;
|
||||
|
||||
export function createAction<Input, Payload>(
|
||||
actionType: string,
|
||||
payloadCreator?: PayloadCreator<Input, Payload>
|
||||
): (...args: Input[]) => Action<Payload>;
|
||||
|
||||
export function createAction<Input, Payload, Meta>(
|
||||
actionType: string,
|
||||
payloadCreator: PayloadCreator<Input, Payload>,
|
||||
metaCreator: MetaCreator<Input, Meta>
|
||||
): (...args: Input[]) => ActionMeta<Payload, Meta>;
|
||||
|
||||
export function handleAction<StateAndPayload>(
|
||||
actionType: { toString: () => string },
|
||||
reducer: Reducer<StateAndPayload, StateAndPayload> | ReducerMap<StateAndPayload, StateAndPayload>
|
||||
): Reducer<StateAndPayload, StateAndPayload>;
|
||||
payloadCreator: (...args: any[]) => Payload,
|
||||
metaCreator: (...args: any[]) => Meta
|
||||
): (...args: any[]) => ActionMeta<Payload, Meta>;
|
||||
|
||||
export function handleAction<State, Payload>(
|
||||
actionType: { toString: () => string },
|
||||
actionType: { toString(): string },
|
||||
reducer: Reducer<State, Payload> | ReducerMap<State, Payload>
|
||||
): Reducer<State, Payload>;
|
||||
|
||||
export function handleAction<State, Payload, Meta>(
|
||||
actionType: { toString: () => string },
|
||||
actionType: { toString(): string },
|
||||
reducer: ReducerMeta<State, Payload, Meta> | ReducerMap<State, Payload>
|
||||
): Reducer<State, Payload>;
|
||||
|
||||
export function handleActions<StateAndPayload>(
|
||||
reducerMap: ReducerMap<StateAndPayload, StateAndPayload>,
|
||||
initialState?: StateAndPayload
|
||||
): Reducer<StateAndPayload, StateAndPayload>;
|
||||
|
||||
export function handleActions<State, Payload>(
|
||||
reducerMap: ReducerMap<State, Payload>,
|
||||
initialState?: State
|
||||
): Reducer<State, Payload>;
|
||||
|
||||
export function combineActions(
|
||||
...actionTypes: { toString: () => string }[]
|
||||
): { toString: () => string };
|
||||
...actionTypes: { toString(): string }[]
|
||||
): { toString(): string };
|
||||
}
|
||||
|
||||
|
||||
@@ -85,10 +85,10 @@ const typedActionHandler = ReduxActions.handleAction<TypedState, TypedPayload>(
|
||||
typedState = typedActionHandler({ value: 0 }, typedIncrementAction());
|
||||
|
||||
const typedIncrementByActionWithMeta: (value: number) => ReduxActions.ActionMeta<TypedPayload, MetaType> =
|
||||
ReduxActions.createAction<number, TypedPayload, MetaType>(
|
||||
ReduxActions.createAction<TypedPayload, MetaType>(
|
||||
'INCREMENT_BY',
|
||||
amount => ({ increase: amount }),
|
||||
amount => ({ remote: true })
|
||||
amount => ({ remote: true })
|
||||
);
|
||||
|
||||
const typedActionHandlerWithReduceMap = ReduxActions.handleAction<TypedState, TypedPayload>(
|
||||
|
||||
Reference in New Issue
Block a user