From 27a41c7a1bb9d09e6b24dfcf9af3e92175536da1 Mon Sep 17 00:00:00 2001 From: Paulo Cesar Date: Sat, 24 Dec 2016 16:46:26 -0200 Subject: [PATCH 1/3] add inferred parameters in createAction --- redux-actions/index.d.ts | 55 ++++++++++++++++++++-------- redux-actions/redux-actions-tests.ts | 37 +++++++++++++++++++ redux-actions/tslint.json | 1 + scripts/new-package.js | 1 + 4 files changed, 78 insertions(+), 16 deletions(-) create mode 100644 redux-actions/tslint.json diff --git a/redux-actions/index.d.ts b/redux-actions/index.d.ts index b5415bca9d..54f373ead0 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 @@ -26,18 +26,46 @@ declare namespace ReduxActions { [actionType: string]: Reducer; } - 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; + + /** argument inferring borrowed from lodash definitions */ + type Function0 = () => R; + type Function1 = (t1: T1) => R; + type Function2 = (t1: T1, t2: T2) => R; + type Function3 = (t1: T1, t2: T2, t3: T3) => R; + type Function4 = (t1: T1, t2: T2, t3: T3, t4: T4) => R; + type FunctionAny = (...args: any[]) => R; export function createAction( actionType: string, - payloadCreator?: (...args: any[]) => Payload, - ): (...args: any[]) => Action; + payloadCreator: Function0 + ): Function0>; + + export function createAction( + actionType: string, + payloadCreator: Function1 + ): Function1>; + + export function createAction( + actionType: string, + payloadCreator: Function2 + ): Function2>; + + export function createAction( + actionType: string, + payloadCreator: Function3 + ): Function3>; + + export function createAction( + actionType: string, + payloadCreator: Function4 + ): Function4>; + + export function createAction( + actionType: string + ): FunctionAny>; export function createAction( actionType: string, @@ -45,13 +73,8 @@ 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 }, + actionType: string | Function0> | Function1> | Function2> | Function3> | Function4> | FunctionAny>, reducer: Reducer | ReducerMap ): Reducer; @@ -71,6 +94,6 @@ declare namespace ReduxActions { ): Reducer; export function combineActions( - ...actionTypes: { toString(): string }[] + ...actionTypes: Array<{ toString(): string }> ): { toString(): string }; } diff --git a/redux-actions/redux-actions-tests.ts b/redux-actions/redux-actions-tests.ts index 4e585f9d9a..65ef3282ed 100644 --- a/redux-actions/redux-actions-tests.ts +++ b/redux-actions/redux-actions-tests.ts @@ -101,3 +101,40 @@ 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 } +}) + +/* can't do this until it lands in 2.2 +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 diff --git a/scripts/new-package.js b/scripts/new-package.js index e235714fe0..c3fb666592 100644 --- a/scripts/new-package.js +++ b/scripts/new-package.js @@ -74,6 +74,7 @@ function getTSConfig() { "module": "commonjs", "target": "es6", "noImplicitAny": true, + "noImplicitThis": true, "strictNullChecks": true, "baseUrl": "../", "typeRoots": [ From ab6200ebeea6e486e2ec1e7caa89295e9998885b Mon Sep 17 00:00:00 2001 From: Paulo Cesar Date: Sun, 25 Dec 2016 08:13:09 -0200 Subject: [PATCH 2/3] meaningful function names and better next/throw --- redux-actions/index.d.ts | 66 +++++++++++++++++----------- redux-actions/redux-actions-tests.ts | 16 +++++-- 2 files changed, 53 insertions(+), 29 deletions(-) diff --git a/redux-actions/index.d.ts b/redux-actions/index.d.ts index 54f373ead0..bd195c1c18 100644 --- a/redux-actions/index.d.ts +++ b/redux-actions/index.d.ts @@ -23,49 +23,65 @@ declare namespace ReduxActions { } interface ReducerMap { - [actionType: string]: Reducer; + [actionType: string]: Reducer | ReducerNextThrow; } + interface ReducerMapMeta { + [actionType: string]: Reducer | ReducerNextThrow; + } + + 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 Function0 = () => R; - type Function1 = (t1: T1) => R; - type Function2 = (t1: T1, t2: T2) => R; - type Function3 = (t1: T1, t2: T2, t3: T3) => R; - type Function4 = (t1: T1, t2: T2, t3: T3, t4: T4) => R; - type FunctionAny = (...args: any[]) => R; + 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: Function0 - ): Function0>; + payloadCreator: ActionFunction0 + ): ActionFunction0>; export function createAction( actionType: string, - payloadCreator: Function1 - ): Function1>; + payloadCreator: ActionFunction1 + ): ActionFunction1>; export function createAction( actionType: string, - payloadCreator: Function2 - ): Function2>; + payloadCreator: ActionFunction2 + ): ActionFunction2>; export function createAction( actionType: string, - payloadCreator: Function3 - ): Function3>; + payloadCreator: ActionFunction3 + ): ActionFunction3>; export function createAction( actionType: string, - payloadCreator: Function4 - ): Function4>; + payloadCreator: ActionFunction4 + ): ActionFunction4>; export function createAction( actionType: string - ): FunctionAny>; + ): ActionFunctionAny>; export function createAction( actionType: string, @@ -74,26 +90,26 @@ declare namespace ReduxActions { ): (...args: any[]) => ActionMeta; export function handleAction( - actionType: string | Function0> | Function1> | Function2> | Function3> | Function4> | FunctionAny>, - 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: Array<{ toString(): string }> - ): { toString(): string }; + ...actionTypes: Array> + ): Array>; } diff --git a/redux-actions/redux-actions-tests.ts b/redux-actions/redux-actions-tests.ts index 65ef3282ed..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' }); @@ -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 {} From 126dc5b597174fdef031ba11e28d1dd6a8ca2352 Mon Sep 17 00:00:00 2001 From: Paulo Cesar Date: Sun, 25 Dec 2016 08:48:31 -0200 Subject: [PATCH 3/3] fix missing semicolon --- redux-actions/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/redux-actions/index.d.ts b/redux-actions/index.d.ts index bd195c1c18..6776f2b621 100644 --- a/redux-actions/index.d.ts +++ b/redux-actions/index.d.ts @@ -40,7 +40,7 @@ declare namespace ReduxActions { throw?(state: State, action: ActionMeta): State; } - type ActionFunctions = ActionFunction0> | ActionFunction1> | ActionFunction2> | ActionFunction3> | ActionFunction4> | ActionFunctionAny> + type ActionFunctions = ActionFunction0> | ActionFunction1> | ActionFunction2> | ActionFunction3> | ActionFunction4> | ActionFunctionAny>; type Reducer = (state: State, action: Action) => State;