mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-24 05:06:02 +08:00
[redux-actions@2.3] Added type for options (#29332)
This commit is contained in:
committed by
Andy
parent
2d8bcf15b9
commit
4070a91a41
19
types/redux-actions/index.d.ts
vendored
19
types/redux-actions/index.d.ts
vendored
@@ -4,6 +4,7 @@
|
||||
// Alex Gorbatchev <https://github.com/alexgorbatchev>,
|
||||
// Alec Hill <https://github.com/alechill>
|
||||
// Alexey Pelykh <https://github.com/alexey-pelykh>
|
||||
// Thiago de Andrade <https://github.com/7hi4g0>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.2
|
||||
|
||||
@@ -155,19 +156,27 @@ export function handleAction<State, Payload, Meta>(
|
||||
initialState: State
|
||||
): Reducer<State, Payload>;
|
||||
|
||||
export interface Options {
|
||||
prefix?: string;
|
||||
namespace?: string;
|
||||
}
|
||||
|
||||
export function handleActions<StateAndPayload>(
|
||||
reducerMap: ReducerMap<StateAndPayload, StateAndPayload>,
|
||||
initialState: StateAndPayload
|
||||
initialState: StateAndPayload,
|
||||
options?: Options
|
||||
): Reducer<StateAndPayload, StateAndPayload>;
|
||||
|
||||
export function handleActions<State, Payload>(
|
||||
reducerMap: ReducerMap<State, Payload>,
|
||||
initialState: State
|
||||
initialState: State,
|
||||
options?: Options
|
||||
): Reducer<State, Payload>;
|
||||
|
||||
export function handleActions<State, Payload, Meta>(
|
||||
reducerMap: ReducerMapMeta<State, Payload, Meta>,
|
||||
initialState: State
|
||||
initialState: State,
|
||||
options?: Options
|
||||
): ReducerMeta<State, Payload, Meta>;
|
||||
|
||||
// https://github.com/redux-utilities/redux-actions/blob/v2.3.0/src/combineActions.js#L21
|
||||
@@ -183,14 +192,14 @@ export interface ActionMap<Payload, Meta> {
|
||||
|
||||
export function createActions<Payload>(
|
||||
actionMapOrIdentityAction: ActionMap<Payload, any> | string,
|
||||
...identityActions: string[]
|
||||
...identityActions: Array<string | Options>
|
||||
): {
|
||||
[actionName: string]: ActionFunctionAny<Action<Payload>>
|
||||
};
|
||||
|
||||
export function createActions(
|
||||
actionMapOrIdentityAction: ActionMap<any, any> | string,
|
||||
...identityActions: string[]
|
||||
...identityActions: Array<string | Options>
|
||||
): {
|
||||
[actionName: string]: ActionFunctionAny<Action<any>>
|
||||
};
|
||||
|
||||
@@ -60,6 +60,22 @@ const actionsHandlerWithRecursiveReducerMap = ReduxActions.handleActions<number,
|
||||
|
||||
state = actionsHandlerWithRecursiveReducerMap(0, { type: 'ADJUST/UP', payload: 1 });
|
||||
|
||||
const actionsHandlerWithOptions = ReduxActions.handleActions({
|
||||
INCREMENT: (state: number, action: ReduxActions.Action<number>) => state + action.payload,
|
||||
MULTIPLY: (state: number, action: ReduxActions.Action<number>) => state * action.payload
|
||||
}, 0, {prefix: 'TEST'});
|
||||
|
||||
state = actionsHandlerWithOptions(0, { type: 'TEST/INCREMENT' });
|
||||
|
||||
const actionsHandlerWithRecursiveReducerMapAndOptions = ReduxActions.handleActions<number, number>({
|
||||
ADJUST: {
|
||||
UP: (state: number, action: ReduxActions.Action<number>) => state + action.payload,
|
||||
DOWN: (state: number, action: ReduxActions.Action<number>) => state - action.payload,
|
||||
}
|
||||
}, 0, {namespace: '--'});
|
||||
|
||||
state = actionsHandlerWithRecursiveReducerMapAndOptions(0, { type: 'ADJUST--UP', payload: 1 });
|
||||
|
||||
// ----------------------------------------------------------------------------------------------------
|
||||
|
||||
interface TypedState {
|
||||
@@ -257,6 +273,13 @@ const { actions1_actionOne } = ReduxActions.createActions({
|
||||
actions1_actionOne('value', 1).payload; // $ExpectType Actions1Payload
|
||||
actions1_actionOne('value', 1).payload.value === 1;
|
||||
|
||||
const options: ReduxActions.Options = {
|
||||
prefix: 'TEST'
|
||||
};
|
||||
const { actions2_actionOne } = ReduxActions.createActions<any>('ACTION_ONE', options);
|
||||
actions2_actionOne('value').payload; // $ExpectType any
|
||||
actions2_actionOne('value').type === 'TEST/ACTION_ONE';
|
||||
|
||||
ReduxActions.handleAction<{ hello: string }, string>(act1, (state, action) => {
|
||||
return { hello: action.payload };
|
||||
}, {hello: 'greetings'});
|
||||
|
||||
Reference in New Issue
Block a user