mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-05 23:37:04 +00:00
meaningful function names and better next/throw
This commit is contained in:
Vendored
+41
-25
@@ -23,49 +23,65 @@ declare namespace ReduxActions {
|
||||
}
|
||||
|
||||
interface ReducerMap<State, Payload> {
|
||||
[actionType: string]: Reducer<State, Payload>;
|
||||
[actionType: string]: Reducer<State, Payload> | ReducerNextThrow<State, Payload>;
|
||||
}
|
||||
|
||||
interface ReducerMapMeta<State, Payload, Meta> {
|
||||
[actionType: string]: Reducer<State, Payload> | ReducerNextThrow<State, Payload>;
|
||||
}
|
||||
|
||||
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 Function0<R> = () => R;
|
||||
type Function1<T1, R> = (t1: T1) => R;
|
||||
type Function2<T1, T2, R> = (t1: T1, t2: T2) => R;
|
||||
type Function3<T1, T2, T3, R> = (t1: T1, t2: T2, t3: T3) => R;
|
||||
type Function4<T1, T2, T3, T4, R> = (t1: T1, t2: T2, t3: T3, t4: T4) => R;
|
||||
type FunctionAny<R> = (...args: any[]) => R;
|
||||
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: Function0<Payload>
|
||||
): Function0<Action<Payload>>;
|
||||
payloadCreator: ActionFunction0<Payload>
|
||||
): ActionFunction0<Action<Payload>>;
|
||||
|
||||
export function createAction<Payload, Arg1>(
|
||||
actionType: string,
|
||||
payloadCreator: Function1<Arg1, Payload>
|
||||
): Function1<Arg1, Action<Payload>>;
|
||||
payloadCreator: ActionFunction1<Arg1, Payload>
|
||||
): ActionFunction1<Arg1, Action<Payload>>;
|
||||
|
||||
export function createAction<Payload, Arg1, Arg2>(
|
||||
actionType: string,
|
||||
payloadCreator: Function2<Arg1, Arg2, Payload>
|
||||
): Function2<Arg1, Arg2, Action<Payload>>;
|
||||
payloadCreator: ActionFunction2<Arg1, Arg2, Payload>
|
||||
): ActionFunction2<Arg1, Arg2, Action<Payload>>;
|
||||
|
||||
export function createAction<Payload, Arg1, Arg2, Arg3>(
|
||||
actionType: string,
|
||||
payloadCreator: Function3<Arg1, Arg2, Arg3, Payload>
|
||||
): Function3<Arg1, Arg2, Arg3, Action<Payload>>;
|
||||
payloadCreator: ActionFunction3<Arg1, Arg2, Arg3, Payload>
|
||||
): ActionFunction3<Arg1, Arg2, Arg3, Action<Payload>>;
|
||||
|
||||
export function createAction<Payload, Arg1, Arg2, Arg3, Arg4>(
|
||||
actionType: string,
|
||||
payloadCreator: Function4<Arg1, Arg2, Arg3, Arg4, Payload>
|
||||
): Function4<Arg1, Arg2, Arg3, Arg4, Action<Payload>>;
|
||||
payloadCreator: ActionFunction4<Arg1, Arg2, Arg3, Arg4, Payload>
|
||||
): ActionFunction4<Arg1, Arg2, Arg3, Arg4, Action<Payload>>;
|
||||
|
||||
export function createAction<Payload>(
|
||||
actionType: string
|
||||
): FunctionAny<Action<Payload>>;
|
||||
): ActionFunctionAny<Action<Payload>>;
|
||||
|
||||
export function createAction<Payload, Meta>(
|
||||
actionType: string,
|
||||
@@ -74,26 +90,26 @@ declare namespace ReduxActions {
|
||||
): (...args: any[]) => ActionMeta<Payload, Meta>;
|
||||
|
||||
export function handleAction<State, Payload>(
|
||||
actionType: string | Function0<Action<Payload>> | Function1<any, Action<Payload>> | Function2<any, any, Action<Payload>> | Function3<any, any, any, Action<Payload>> | Function4<any, any, any, any, Action<Payload>> | FunctionAny<Action<Payload>>,
|
||||
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: Array<{ 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' });
|
||||
@@ -123,7 +127,11 @@ ReduxActions.handleAction(act3, (state, action) => {
|
||||
return { hello: action.payload.s }
|
||||
})
|
||||
|
||||
/* can't do this until it lands in 2.2
|
||||
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 {}
|
||||
|
||||
Reference in New Issue
Block a user