diff --git a/redux-actions/index.d.ts b/redux-actions/index.d.ts index b5415bca9d..6776f2b621 100644 --- a/redux-actions/index.d.ts +++ b/redux-actions/index.d.ts @@ -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 , Alex Gorbatchev // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped @@ -23,21 +23,65 @@ declare namespace ReduxActions { } interface ReducerMap { - [actionType: string]: Reducer; + [actionType: string]: Reducer | ReducerNextThrow; } - interface Reducer { - (state: State, action: Action): State; + interface ReducerMapMeta { + [actionType: string]: Reducer | ReducerNextThrow; } - interface ReducerMeta extends Reducer { - (state: State, action: ActionMeta): State; + interface ReducerNextThrow { + next?(state: State, action: Action): State; + throw?(state: State, action: Action): State; } + interface ReducerNextThrowMeta { + next?(state: State, action: ActionMeta): State; + throw?(state: State, action: ActionMeta): State; + } + + type ActionFunctions = ActionFunction0> | ActionFunction1> | ActionFunction2> | ActionFunction3> | ActionFunction4> | ActionFunctionAny>; + + type Reducer = (state: State, action: Action) => State; + + type ReducerMeta = (state: State, action: ActionMeta) => State; + + /** argument inferring borrowed from lodash definitions */ + type ActionFunction0 = () => R; + type ActionFunction1 = (t1: T1) => R; + type ActionFunction2 = (t1: T1, t2: T2) => R; + type ActionFunction3 = (t1: T1, t2: T2, t3: T3) => R; + type ActionFunction4 = (t1: T1, t2: T2, t3: T3, t4: T4) => R; + type ActionFunctionAny = (...args: any[]) => R; + export function createAction( actionType: string, - payloadCreator?: (...args: any[]) => Payload, - ): (...args: any[]) => Action; + payloadCreator: ActionFunction0 + ): ActionFunction0>; + + export function createAction( + actionType: string, + payloadCreator: ActionFunction1 + ): ActionFunction1>; + + export function createAction( + actionType: string, + payloadCreator: ActionFunction2 + ): ActionFunction2>; + + export function createAction( + actionType: string, + payloadCreator: ActionFunction3 + ): ActionFunction3>; + + export function createAction( + actionType: string, + payloadCreator: ActionFunction4 + ): ActionFunction4>; + + export function createAction( + actionType: string + ): ActionFunctionAny>; export function createAction( actionType: string, @@ -45,32 +89,27 @@ declare namespace ReduxActions { metaCreator: (...args: any[]) => Meta ): (...args: any[]) => ActionMeta; - export function handleAction( - actionType: { toString: () => string }, - reducer: Reducer | ReducerMap - ): Reducer; - export function handleAction( - actionType: { toString(): string }, - reducer: Reducer | ReducerMap + actionType: string | ActionFunctions, + reducer: Reducer | ReducerNextThrow ): Reducer; export function handleAction( actionType: { toString(): string }, - reducer: ReducerMeta | ReducerMap + reducer: ReducerMeta | ReducerNextThrowMeta ): Reducer; export function handleActions( reducerMap: ReducerMap, - initialState?: StateAndPayload + initialState: StateAndPayload ): Reducer; export function handleActions( reducerMap: ReducerMap, - initialState?: State + initialState: State ): Reducer; export function combineActions( - ...actionTypes: { toString(): string }[] - ): { toString(): string }; + ...actionTypes: Array> + ): Array>; } diff --git a/redux-actions/redux-actions-tests.ts b/redux-actions/redux-actions-tests.ts index 4e585f9d9a..f5a2d41e18 100644 --- a/redux-actions/redux-actions-tests.ts +++ b/redux-actions/redux-actions-tests.ts @@ -34,13 +34,17 @@ state = actionHandlerWithReduceMap(0, multiplyAction(10)); const actionsHandler = ReduxActions.handleActions({ 'INCREMENT': (state: number, action: ReduxActions.Action) => state + action.payload, 'MULTIPLY': (state: number, action: ReduxActions.Action) => state * action.payload -}); +}, 0); state = actionsHandler(0, { type: 'INCREMENT' }); const actionsHandlerWithInitialState = ReduxActions.handleActions({ - 'INCREMENT': (state: number, action: ReduxActions.Action) => state + action.payload, - 'MULTIPLY': (state: number, action: ReduxActions.Action) => state * action.payload + 'INCREMENT': { + next: (state: number, action: ReduxActions.Action) => state + action.payload, + }, + 'MULTIPLY': { + next: (state: number, action: ReduxActions.Action) => state * action.payload + } }, 0); state = actionsHandlerWithInitialState(0, { type: 'INCREMENT' }); @@ -101,3 +105,44 @@ const typedActionHandlerWithReduceMap = ReduxActions.handleAction('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 {} +})*/ diff --git a/redux-actions/tslint.json b/redux-actions/tslint.json new file mode 100644 index 0000000000..2221e40e4a --- /dev/null +++ b/redux-actions/tslint.json @@ -0,0 +1 @@ +{ "extends": "../tslint.json" } \ No newline at end of file