From 0a8a9566a3bbb8fa450cefb7829f81d01893d08d Mon Sep 17 00:00:00 2001 From: Kyle Herock Date: Sat, 12 Nov 2016 20:54:02 -0500 Subject: [PATCH] Improved redux-actions type inferencing of createAction and handleAction --- redux-actions/index.d.ts | 71 ++++++++++------------------ redux-actions/redux-actions-tests.ts | 4 +- 2 files changed, 26 insertions(+), 49 deletions(-) diff --git a/redux-actions/index.d.ts b/redux-actions/index.d.ts index 25424e7040..5247e21704 100644 --- a/redux-actions/index.d.ts +++ b/redux-actions/index.d.ts @@ -9,80 +9,57 @@ declare namespace ReduxActions { // FSA-compliant action. // See: https://github.com/acdlite/flux-standard-action interface BaseAction { - type: string + type: string; } export interface Action extends BaseAction { - payload?: Payload - error?: boolean - meta?: any + payload?: Payload; + error?: boolean; } export interface ActionMeta extends Action { - meta: Meta + meta: Meta; } - type PayloadCreator = (...args: Input[]) => Payload; + interface ReducerMap { + [actionType: string]: Reducer; + } - type MetaCreator = (...args: Input[]) => Payload; + interface Reducer { + (state: State, action: Action): State; + } - type Reducer = (state: State, action: Action) => State; + interface ReducerMeta extends Reducer { + (state: State, action: ActionMeta): State; + } - type ReducerMeta = (state: State, action: ActionMeta) => State; - - type ReducerMap = { - [actionType: string]: Reducer - }; - - export function createAction( + export function createAction( actionType: string, - payloadCreator?: PayloadCreator, - metaCreator?: MetaCreator - ): (...args: any[]) => Action; + payloadCreator?: (...args: any[]) => Payload, + ): (...args: any[]) => Action; - export function createAction( + export function createAction( actionType: string, - payloadCreator?: PayloadCreator - ): (...args: InputAndPayload[]) => Action; - - export function createAction( - actionType: string, - payloadCreator?: PayloadCreator - ): (...args: Input[]) => Action; - - export function createAction( - actionType: string, - payloadCreator: PayloadCreator, - metaCreator: MetaCreator - ): (...args: Input[]) => ActionMeta; - - export function handleAction( - actionType: { toString: () => string }, - reducer: Reducer | ReducerMap - ): Reducer; + payloadCreator: (...args: any[]) => Payload, + metaCreator: (...args: any[]) => Meta + ): (...args: any[]) => ActionMeta; export function handleAction( - actionType: { toString: () => string }, + actionType: { toString(): string }, reducer: Reducer | ReducerMap ): Reducer; export function handleAction( - actionType: { toString: () => string }, + actionType: { toString(): string }, reducer: ReducerMeta | ReducerMap ): Reducer; - export function handleActions( - reducerMap: ReducerMap, - initialState?: StateAndPayload - ): Reducer; - export function handleActions( reducerMap: ReducerMap, initialState?: State ): Reducer; export function combineActions( - ...actionTypes: { toString: () => string }[] - ): { toString: () => string }; + ...actionTypes: { toString(): string }[] + ): { toString(): string }; } - diff --git a/redux-actions/redux-actions-tests.ts b/redux-actions/redux-actions-tests.ts index 5cef02f5fc..4e585f9d9a 100644 --- a/redux-actions/redux-actions-tests.ts +++ b/redux-actions/redux-actions-tests.ts @@ -85,10 +85,10 @@ const typedActionHandler = ReduxActions.handleAction( typedState = typedActionHandler({ value: 0 }, typedIncrementAction()); const typedIncrementByActionWithMeta: (value: number) => ReduxActions.ActionMeta = - ReduxActions.createAction( + ReduxActions.createAction( 'INCREMENT_BY', amount => ({ increase: amount }), - amount => ({ remote: true }) + amount => ({ remote: true }) ); const typedActionHandlerWithReduceMap = ReduxActions.handleAction(