mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2025-10-16 12:05:41 +00:00
strict null checks for redux-actions
This commit is contained in:
parent
e95a4f0f1f
commit
46e664589a
17
types/redux-actions/index.d.ts
vendored
17
types/redux-actions/index.d.ts
vendored
@ -5,6 +5,7 @@
|
||||
// Alec Hill <https://github.com/alechill>
|
||||
// Alexey Pelykh <https://github.com/alexey-pelykh>
|
||||
// Thiago de Andrade <https://github.com/7hi4g0>
|
||||
// Ziyu <https://github.com/oddui>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.2
|
||||
|
||||
@ -17,7 +18,7 @@ export interface BaseAction {
|
||||
}
|
||||
|
||||
export interface Action<Payload> extends BaseAction {
|
||||
payload?: Payload;
|
||||
payload: Payload;
|
||||
error?: boolean;
|
||||
}
|
||||
|
||||
@ -66,6 +67,10 @@ export type Reducer<State, Payload> = (state: State, action: Action<Payload>) =>
|
||||
|
||||
export type ReducerMeta<State, Payload, Meta> = (state: State, action: ActionMeta<Payload, Meta>) => State;
|
||||
|
||||
export type ReduxReducer<State, Payload> = (state: State | undefined, action: Action<Payload>) => State;
|
||||
|
||||
export type ReduxReducerMeta<State, Payload, Meta> = (state: State | undefined, action: ActionMeta<Payload, Meta>) => State;
|
||||
|
||||
/** argument inferring borrowed from lodash definitions */
|
||||
export type ActionFunction0<R> = () => R;
|
||||
export type ActionFunction1<T1, R> = (t1: T1) => R;
|
||||
@ -148,13 +153,13 @@ export function handleAction<State, Payload>(
|
||||
actionType: string | ActionFunctions<Payload> | CombinedActionType,
|
||||
reducer: Reducer<State, Payload> | ReducerNextThrow<State, Payload>,
|
||||
initialState: State
|
||||
): Reducer<State, Payload>;
|
||||
): ReduxReducer<State, Payload>;
|
||||
|
||||
export function handleAction<State, Payload, Meta>(
|
||||
actionType: string | ActionWithMetaFunctions<Payload, Meta> | CombinedActionType,
|
||||
reducer: ReducerMeta<State, Payload, Meta> | ReducerNextThrowMeta<State, Payload, Meta>,
|
||||
initialState: State
|
||||
): Reducer<State, Payload>;
|
||||
): ReduxReducerMeta<State, Payload, Meta>;
|
||||
|
||||
export interface Options {
|
||||
prefix?: string;
|
||||
@ -165,19 +170,19 @@ export function handleActions<StateAndPayload>(
|
||||
reducerMap: ReducerMap<StateAndPayload, StateAndPayload>,
|
||||
initialState: StateAndPayload,
|
||||
options?: Options
|
||||
): Reducer<StateAndPayload, StateAndPayload>;
|
||||
): ReduxReducer<StateAndPayload, StateAndPayload>;
|
||||
|
||||
export function handleActions<State, Payload>(
|
||||
reducerMap: ReducerMap<State, Payload>,
|
||||
initialState: State,
|
||||
options?: Options
|
||||
): Reducer<State, Payload>;
|
||||
): ReduxReducer<State, Payload>;
|
||||
|
||||
export function handleActions<State, Payload, Meta>(
|
||||
reducerMap: ReducerMapMeta<State, Payload, Meta>,
|
||||
initialState: State,
|
||||
options?: Options
|
||||
): ReducerMeta<State, Payload, Meta>;
|
||||
): ReduxReducerMeta<State, Payload, Meta>;
|
||||
|
||||
// https://github.com/redux-utilities/redux-actions/blob/v2.3.0/src/combineActions.js#L21
|
||||
export function combineActions(...actionTypes: Array<ActionFunctions<any> | string | symbol>): CombinedActionType;
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
import * as ReduxActions from 'redux-actions';
|
||||
|
||||
let state: number;
|
||||
const minimalAction: ReduxActions.BaseAction = { type: 'INCREMENT' };
|
||||
|
||||
const incrementAction: () => ReduxActions.Action<number> = ReduxActions.createAction<number>(
|
||||
'INCREMENT', () => 1
|
||||
@ -11,14 +10,13 @@ const multiplyAction: (...args: number[]) => ReduxActions.Action<number> = Redux
|
||||
'MULTIPLY'
|
||||
);
|
||||
|
||||
const action: ReduxActions.Action<number> = incrementAction();
|
||||
|
||||
const actionHandler = ReduxActions.handleAction<number, number>(
|
||||
'INCREMENT',
|
||||
(state: number, action: ReduxActions.Action<number>) => state + action.payload,
|
||||
0
|
||||
);
|
||||
|
||||
state = actionHandler(undefined, incrementAction());
|
||||
state = actionHandler(0, incrementAction());
|
||||
|
||||
const actionHandlerWithReduceMap = ReduxActions.handleAction<number, number>(
|
||||
@ -31,6 +29,7 @@ const actionHandlerWithReduceMap = ReduxActions.handleAction<number, number>(
|
||||
0
|
||||
);
|
||||
|
||||
state = actionHandlerWithReduceMap(undefined, multiplyAction(10));
|
||||
state = actionHandlerWithReduceMap(0, multiplyAction(10));
|
||||
|
||||
const actionsHandler = ReduxActions.handleActions({
|
||||
@ -38,7 +37,8 @@ const actionsHandler = ReduxActions.handleActions({
|
||||
MULTIPLY: (state: number, action: ReduxActions.Action<number>) => state * action.payload
|
||||
}, 0);
|
||||
|
||||
state = actionsHandler(0, { type: 'INCREMENT' });
|
||||
state = actionsHandler(undefined, incrementAction());
|
||||
state = actionsHandler(0, incrementAction());
|
||||
|
||||
const actionsHandlerWithInitialState = ReduxActions.handleActions({
|
||||
INCREMENT: {
|
||||
@ -49,7 +49,8 @@ const actionsHandlerWithInitialState = ReduxActions.handleActions({
|
||||
}
|
||||
}, 0);
|
||||
|
||||
state = actionsHandlerWithInitialState(0, { type: 'INCREMENT' });
|
||||
state = actionsHandlerWithInitialState(undefined, incrementAction());
|
||||
state = actionsHandlerWithInitialState(0, incrementAction());
|
||||
|
||||
const actionsHandlerWithRecursiveReducerMap = ReduxActions.handleActions<number, number>({
|
||||
ADJUST: {
|
||||
@ -58,6 +59,7 @@ const actionsHandlerWithRecursiveReducerMap = ReduxActions.handleActions<number,
|
||||
}
|
||||
}, 0);
|
||||
|
||||
state = actionsHandlerWithRecursiveReducerMap(undefined, { type: 'ADJUST/UP', payload: 1 });
|
||||
state = actionsHandlerWithRecursiveReducerMap(0, { type: 'ADJUST/UP', payload: 1 });
|
||||
|
||||
const actionsHandlerWithOptions = ReduxActions.handleActions({
|
||||
@ -65,7 +67,8 @@ const actionsHandlerWithOptions = ReduxActions.handleActions({
|
||||
MULTIPLY: (state: number, action: ReduxActions.Action<number>) => state * action.payload
|
||||
}, 0, {prefix: 'TEST'});
|
||||
|
||||
state = actionsHandlerWithOptions(0, { type: 'TEST/INCREMENT' });
|
||||
state = actionsHandlerWithOptions(undefined, { type: 'TEST/INCREMENT', payload: 1 });
|
||||
state = actionsHandlerWithOptions(0, { type: 'TEST/INCREMENT', payload: 1 });
|
||||
|
||||
const actionsHandlerWithRecursiveReducerMapAndOptions = ReduxActions.handleActions<number, number>({
|
||||
ADJUST: {
|
||||
@ -74,6 +77,7 @@ const actionsHandlerWithRecursiveReducerMapAndOptions = ReduxActions.handleActio
|
||||
}
|
||||
}, 0, {namespace: '--'});
|
||||
|
||||
state = actionsHandlerWithRecursiveReducerMapAndOptions(undefined, { type: 'ADJUST--UP', payload: 1 });
|
||||
state = actionsHandlerWithRecursiveReducerMapAndOptions(0, { type: 'ADJUST--UP', payload: 1 });
|
||||
|
||||
// ----------------------------------------------------------------------------------------------------
|
||||
@ -117,6 +121,7 @@ const typedActionHandler = ReduxActions.handleAction<TypedState, TypedPayload>(
|
||||
const actionNoArgs = typedIncrementAction();
|
||||
actionNoArgs.payload.increase = 1;
|
||||
|
||||
typedState = typedActionHandler(undefined, actionNoArgs);
|
||||
typedState = typedActionHandler({ value: 0 }, actionNoArgs);
|
||||
|
||||
const typedIncrementAction1TypedArg: (value: number) =>
|
||||
@ -144,6 +149,7 @@ const typedActionHandlerReducerMap = ReduxActions.handleActions(
|
||||
{value: 1}
|
||||
);
|
||||
|
||||
typedState = typedActionHandlerReducerMap(undefined, actionFrom1Arg);
|
||||
typedState = typedActionHandlerReducerMap({ value: 0 }, actionFrom1Arg);
|
||||
|
||||
const typedIncrementByActionWithMetaAnyArgs: (...args: any[]) => ReduxActions.ActionMeta<TypedPayload, MetaType> =
|
||||
@ -167,6 +173,7 @@ const typedActionHandlerWithMeta = ReduxActions.handleAction<TypedState, TypedPa
|
||||
{value: 1}
|
||||
);
|
||||
|
||||
typedState = typedActionHandlerWithMeta(undefined, typedIncrementByActionWithMetaAnyArgs(10));
|
||||
typedState = typedActionHandlerWithMeta({ value: 0 }, typedIncrementByActionWithMetaAnyArgs(10));
|
||||
|
||||
const typedActionHandlerReducerMetaMap = ReduxActions.handleActions<TypedState, TypedPayload, MetaType>(
|
||||
@ -181,6 +188,7 @@ const typedActionHandlerReducerMetaMap = ReduxActions.handleActions<TypedState,
|
||||
{value: 1}
|
||||
);
|
||||
|
||||
typedState = typedActionHandlerReducerMetaMap(undefined, actionMetaFromAnyArgs);
|
||||
typedState = typedActionHandlerReducerMetaMap({ value: 0 }, actionMetaFromAnyArgs);
|
||||
|
||||
const typedActionWithMeta1TypedArg: (value: number) => ReduxActions.ActionMeta<TypedPayload, MetaType> =
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": false,
|
||||
"strictNullChecks": true,
|
||||
"strictFunctionTypes": false,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
|
||||
Loading…
Reference in New Issue
Block a user