mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-05 23:37:04 +00:00
Merge pull request #13542 from pocesar/redux-actions
add inferred parameters in createAction for redux-actions
This commit is contained in:
Vendored
+59
-20
@@ -1,4 +1,4 @@
|
||||
// Type definitions for redux-actions v0.8.0
|
||||
// Type definitions for redux-actions 1.2
|
||||
// Project: https://github.com/acdlite/redux-actions
|
||||
// Definitions by: Jack Hsu <https://github.com/jaysoo>, Alex Gorbatchev <https://github.com/alexgorbatchev>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
@@ -23,21 +23,65 @@ declare namespace ReduxActions {
|
||||
}
|
||||
|
||||
interface ReducerMap<State, Payload> {
|
||||
[actionType: string]: Reducer<State, Payload>;
|
||||
[actionType: string]: Reducer<State, Payload> | ReducerNextThrow<State, Payload>;
|
||||
}
|
||||
|
||||
interface Reducer<State, Payload> {
|
||||
(state: State, action: Action<Payload>): State;
|
||||
interface ReducerMapMeta<State, Payload, Meta> {
|
||||
[actionType: string]: Reducer<State, Payload> | ReducerNextThrow<State, Payload>;
|
||||
}
|
||||
|
||||
interface ReducerMeta<State, Payload, Meta> extends Reducer<State, Payload> {
|
||||
(state: State, action: ActionMeta<Payload, Meta>): State;
|
||||
interface ReducerNextThrow<State, Payload> {
|
||||
next?(state: State, action: Action<Payload>): State;
|
||||
throw?(state: State, action: Action<Payload>): State;
|
||||
}
|
||||
|
||||
interface ReducerNextThrowMeta<State, Payload, Meta> {
|
||||
next?(state: State, action: ActionMeta<Payload, Meta>): State;
|
||||
throw?(state: State, action: ActionMeta<Payload, Meta>): State;
|
||||
}
|
||||
|
||||
type ActionFunctions<Payload> = ActionFunction0<Action<Payload>> | ActionFunction1<any, Action<Payload>> | ActionFunction2<any, any, Action<Payload>> | ActionFunction3<any, any, any, Action<Payload>> | ActionFunction4<any, any, any, any, Action<Payload>> | ActionFunctionAny<Action<Payload>>;
|
||||
|
||||
type Reducer<State, Payload> = (state: State, action: Action<Payload>) => State;
|
||||
|
||||
type ReducerMeta<State, Payload, Meta> = (state: State, action: ActionMeta<Payload, Meta>) => State;
|
||||
|
||||
/** argument inferring borrowed from lodash definitions */
|
||||
type ActionFunction0<R> = () => R;
|
||||
type ActionFunction1<T1, R> = (t1: T1) => R;
|
||||
type ActionFunction2<T1, T2, R> = (t1: T1, t2: T2) => R;
|
||||
type ActionFunction3<T1, T2, T3, R> = (t1: T1, t2: T2, t3: T3) => R;
|
||||
type ActionFunction4<T1, T2, T3, T4, R> = (t1: T1, t2: T2, t3: T3, t4: T4) => R;
|
||||
type ActionFunctionAny<R> = (...args: any[]) => R;
|
||||
|
||||
export function createAction<Payload>(
|
||||
actionType: string,
|
||||
payloadCreator?: (...args: any[]) => Payload,
|
||||
): (...args: any[]) => Action<Payload>;
|
||||
payloadCreator: ActionFunction0<Payload>
|
||||
): ActionFunction0<Action<Payload>>;
|
||||
|
||||
export function createAction<Payload, Arg1>(
|
||||
actionType: string,
|
||||
payloadCreator: ActionFunction1<Arg1, Payload>
|
||||
): ActionFunction1<Arg1, Action<Payload>>;
|
||||
|
||||
export function createAction<Payload, Arg1, Arg2>(
|
||||
actionType: string,
|
||||
payloadCreator: ActionFunction2<Arg1, Arg2, Payload>
|
||||
): ActionFunction2<Arg1, Arg2, Action<Payload>>;
|
||||
|
||||
export function createAction<Payload, Arg1, Arg2, Arg3>(
|
||||
actionType: string,
|
||||
payloadCreator: ActionFunction3<Arg1, Arg2, Arg3, Payload>
|
||||
): ActionFunction3<Arg1, Arg2, Arg3, Action<Payload>>;
|
||||
|
||||
export function createAction<Payload, Arg1, Arg2, Arg3, Arg4>(
|
||||
actionType: string,
|
||||
payloadCreator: ActionFunction4<Arg1, Arg2, Arg3, Arg4, Payload>
|
||||
): ActionFunction4<Arg1, Arg2, Arg3, Arg4, Action<Payload>>;
|
||||
|
||||
export function createAction<Payload>(
|
||||
actionType: string
|
||||
): ActionFunctionAny<Action<Payload>>;
|
||||
|
||||
export function createAction<Payload, Meta>(
|
||||
actionType: string,
|
||||
@@ -45,32 +89,27 @@ declare namespace ReduxActions {
|
||||
metaCreator: (...args: any[]) => Meta
|
||||
): (...args: any[]) => ActionMeta<Payload, Meta>;
|
||||
|
||||
export function handleAction<StateAndPayload>(
|
||||
actionType: { toString: () => string },
|
||||
reducer: Reducer<StateAndPayload, StateAndPayload> | ReducerMap<StateAndPayload, StateAndPayload>
|
||||
): Reducer<StateAndPayload, StateAndPayload>;
|
||||
|
||||
export function handleAction<State, Payload>(
|
||||
actionType: { toString(): string },
|
||||
reducer: Reducer<State, Payload> | ReducerMap<State, Payload>
|
||||
actionType: string | ActionFunctions<Payload>,
|
||||
reducer: Reducer<State, Payload> | ReducerNextThrow<State, Payload>
|
||||
): Reducer<State, Payload>;
|
||||
|
||||
export function handleAction<State, Payload, Meta>(
|
||||
actionType: { toString(): string },
|
||||
reducer: ReducerMeta<State, Payload, Meta> | ReducerMap<State, Payload>
|
||||
reducer: ReducerMeta<State, Payload, Meta> | ReducerNextThrowMeta<State, Payload, Meta>
|
||||
): Reducer<State, Payload>;
|
||||
|
||||
export function handleActions<StateAndPayload>(
|
||||
reducerMap: ReducerMap<StateAndPayload, StateAndPayload>,
|
||||
initialState?: StateAndPayload
|
||||
initialState: StateAndPayload
|
||||
): Reducer<StateAndPayload, StateAndPayload>;
|
||||
|
||||
export function handleActions<State, Payload>(
|
||||
reducerMap: ReducerMap<State, Payload>,
|
||||
initialState?: State
|
||||
initialState: State
|
||||
): Reducer<State, Payload>;
|
||||
|
||||
export function combineActions(
|
||||
...actionTypes: { toString(): string }[]
|
||||
): { toString(): string };
|
||||
...actionTypes: Array<ActionFunctions<any>>
|
||||
): Array<ActionFunctions<any>>;
|
||||
}
|
||||
|
||||
@@ -34,13 +34,17 @@ state = actionHandlerWithReduceMap(0, multiplyAction(10));
|
||||
const actionsHandler = ReduxActions.handleActions<number, number>({
|
||||
'INCREMENT': (state: number, action: ReduxActions.Action<number>) => state + action.payload,
|
||||
'MULTIPLY': (state: number, action: ReduxActions.Action<number>) => state * action.payload
|
||||
});
|
||||
}, 0);
|
||||
|
||||
state = actionsHandler(0, { type: 'INCREMENT' });
|
||||
|
||||
const actionsHandlerWithInitialState = ReduxActions.handleActions<number, number>({
|
||||
'INCREMENT': (state: number, action: ReduxActions.Action<number>) => state + action.payload,
|
||||
'MULTIPLY': (state: number, action: ReduxActions.Action<number>) => state * action.payload
|
||||
'INCREMENT': {
|
||||
next: (state: number, action: ReduxActions.Action<number>) => state + action.payload,
|
||||
},
|
||||
'MULTIPLY': {
|
||||
next: (state: number, action: ReduxActions.Action<number>) => state * action.payload
|
||||
}
|
||||
}, 0);
|
||||
|
||||
state = actionsHandlerWithInitialState(0, { type: 'INCREMENT' });
|
||||
@@ -101,3 +105,44 @@ const typedActionHandlerWithReduceMap = ReduxActions.handleAction<TypedState, Ty
|
||||
);
|
||||
|
||||
typedState = typedActionHandlerWithReduceMap({ value: 0 }, typedIncrementByActionWithMeta(10));
|
||||
|
||||
const act = ReduxActions.createAction<string>('ACTION1')
|
||||
act('hello').payload === 'hello'
|
||||
|
||||
const act2 = ReduxActions.createAction('ACTION2', (s: {load: boolean}) => s)
|
||||
act2({load: true}).payload.load == true
|
||||
|
||||
const act3 = ReduxActions.createAction('ACTION3', (s: string) => ({s}))
|
||||
act3('hello').payload.s == 'hello'
|
||||
|
||||
ReduxActions.handleAction<{ hello: string }, string>(act, (state, action) => {
|
||||
return { hello: action.payload }
|
||||
})
|
||||
|
||||
ReduxActions.handleAction<{ hello: { load: boolean } }, { load: boolean }>(act2, (state, action) => {
|
||||
return { hello: action.payload }
|
||||
})
|
||||
|
||||
ReduxActions.handleAction(act3, (state, action) => {
|
||||
return { hello: action.payload.s }
|
||||
})
|
||||
|
||||
ReduxActions.handleAction(ReduxActions.combineActions(act, act3, act2), () => {
|
||||
|
||||
})
|
||||
|
||||
/* can't do this until it lands in 2.2, HKTs
|
||||
ReduxActions.handleAction(act, (state, action) => {
|
||||
action.payload === 'hello'
|
||||
return {}
|
||||
})
|
||||
|
||||
ReduxActions.handleAction(act2, (state, action) => {
|
||||
action.payload.load === true
|
||||
return {}
|
||||
})
|
||||
|
||||
ReduxActions.handleAction(act3, (state, action) => {
|
||||
action.payload.s == 'hello'
|
||||
return {}
|
||||
})*/
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
{ "extends": "../tslint.json" }
|
||||
Reference in New Issue
Block a user