From b466184ff29efb3815974e5ad3e0f25ecf070711 Mon Sep 17 00:00:00 2001 From: Florian Richter Date: Tue, 12 Sep 2017 12:59:32 +0200 Subject: [PATCH 1/2] Reduce strictness of types for redux actions to reduce false positives --- types/redux-actions/index.d.ts | 111 +++++++-------------- types/redux-actions/redux-actions-tests.ts | 16 +-- 2 files changed, 42 insertions(+), 85 deletions(-) diff --git a/types/redux-actions/index.d.ts b/types/redux-actions/index.d.ts index d088592217..d909fcfc7a 100644 --- a/types/redux-actions/index.d.ts +++ b/types/redux-actions/index.d.ts @@ -4,6 +4,7 @@ // Alex Gorbatchev , // Alec Hill // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 export as namespace ReduxActions; @@ -22,12 +23,12 @@ export interface ActionMeta extends Action { meta: Meta; } -export interface ReducerMap { - [actionType: string]: Reducer | ReducerNextThrow; +export interface ReducerMap { + [actionType: string]: Reducer | ReducerNextThrow; } -export interface ReducerMapMeta { - [actionType: string]: ReducerMeta | ReducerNextThrowMeta; +export interface ReducerMapMeta { + [actionType: string]: ReducerMeta | ReducerNextThrowMeta; } export interface ReducerNextThrow { @@ -41,11 +42,7 @@ export interface ReducerNextThrowMeta { } export type ActionFunctions = - ActionFunction0> | - ActionFunction1> | - ActionFunction2> | - ActionFunction3> | - ActionFunction4> | + ActionFunction> | ActionFunctionAny>; export type Reducer = (state: State, action: Action) => State; @@ -53,45 +50,21 @@ export type Reducer = (state: State, action: Action) => export type ReducerMeta = (state: State, action: ActionMeta) => State; /** argument inferring borrowed from lodash definitions */ -export type ActionFunction0 = () => R; -export type ActionFunction1 = (t1: T1) => R; -export type ActionFunction2 = (t1: T1, t2: T2) => R; -export type ActionFunction3 = (t1: T1, t2: T2, t3: T3) => R; -export type ActionFunction4 = (t1: T1, t2: T2, t3: T3, t4: T4) => R; +export type ActionFunction = (t1?: T1, t2?: T2, t3?: T3, t4?: T4) => R; export type ActionFunctionAny = (...args: any[]) => R; export function createAction( actionType: string -): ActionFunction0>; +): ActionFunction>; -export function createAction( +export function createAction( actionType: string, - 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>; + payloadCreator: ActionFunction +): ActionFunction, Arg1, Arg2, Arg3, Arg4>; export function createAction( actionType: string -): ActionFunction1>; +): ActionFunction, Payload>; export function createAction( actionType: string, @@ -99,29 +72,11 @@ export function createAction( metaCreator: ActionFunctionAny ): ActionFunctionAny>; -export function createAction( - actionType: string, - payloadCreator: ActionFunction1, - metaCreator: ActionFunction1 -): ActionFunction1>; - -export function createAction( - actionType: string, - payloadCreator: ActionFunction2, - metaCreator: ActionFunction2 -): ActionFunction2>; - -export function createAction( - actionType: string, - payloadCreator: ActionFunction3, - metaCreator: ActionFunction3 -): ActionFunction3>; - export function createAction( actionType: string, - payloadCreator: ActionFunction4, - metaCreator: ActionFunction4 -): ActionFunction4>; + payloadCreator: ActionFunction, + metaCreator: ActionFunction +): ActionFunction, Arg1, Arg2, Arg3, Arg4>; export function handleAction( actionType: string | ActionFunctions, @@ -135,34 +90,36 @@ export function handleAction( initialState: State ): Reducer; -export function handleActions( - reducerMap: ReducerMap, - initialState: StateAndPayload -): Reducer; - -export function handleActions( - reducerMap: ReducerMap, +export function handleActions( + reducerMap: ReducerMap, initialState: State -): Reducer; +): Reducer; -export function handleActions( - reducerMap: ReducerMapMeta, +export function handleActions( + reducerMap: ReducerMapMeta, initialState: State -): ReducerMeta; +): ReducerMeta; export function combineActions(...actionTypes: Array | string>): string; -export interface ActionMap { +export interface ActionMap { [actionType: string]: - ActionMap | - ActionFunctionAny | - [ActionFunctionAny, ActionFunctionAny] | - undefined; + ActionMap | + ActionFunctionAny | + [ActionFunctionAny, ActionFunctionAny] | + undefined; } +export function createActions( + actionMap: ActionMap, + ...identityActions: string[] +): { + [actionName: string]: ActionFunctionAny> +}; + export function createActions( actionMap: ActionMap, ...identityActions: string[] ): { - [actionName: string]: ActionFunctionAny> + [actionName: string]: ActionFunctionAny> }; diff --git a/types/redux-actions/redux-actions-tests.ts b/types/redux-actions/redux-actions-tests.ts index 003599c8bc..e12efd461f 100644 --- a/types/redux-actions/redux-actions-tests.ts +++ b/types/redux-actions/redux-actions-tests.ts @@ -1,4 +1,4 @@ -import * as ReduxActions from "redux-actions"; +import * as ReduxActions from 'redux-actions'; let state: number; const minimalAction: ReduxActions.BaseAction = { type: 'INCREMENT' }; @@ -33,14 +33,14 @@ const actionHandlerWithReduceMap = ReduxActions.handleAction( state = actionHandlerWithReduceMap(0, multiplyAction(10)); -const actionsHandler = ReduxActions.handleActions({ +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({ +const actionsHandlerWithInitialState = ReduxActions.handleActions({ INCREMENT: { next: (state: number, action: ReduxActions.Action) => state + action.payload, }, @@ -112,9 +112,9 @@ ReduxActions.Action = ReduxActions.createAction( +const typedActionHandlerReducerMap = ReduxActions.handleActions( { - INCREMENT: (state: TypedState, action: ReduxActions.Action) => ({ value: state.value + 1 }) + INCREMENT: (state: TypedState, action: ReduxActions.Action) => ({ value: state.value + 1 }) }, {value: 1} ); @@ -144,7 +144,7 @@ const typedActionHandlerWithMeta = ReduxActions.handleAction( +const typedActionHandlerReducerMetaMap = ReduxActions.handleActions( { INCREMENT_BY: { next(state: TypedState, action: ReduxActions.ActionMeta) { @@ -159,7 +159,7 @@ const typedActionHandlerReducerMetaMap = ReduxActions.handleActions ReduxActions.ActionMeta = - ReduxActions.createAction( + ReduxActions.createAction( 'INCREMENT_BY', amount => ({ increase: amount }), amount => ({ remote: true }) @@ -172,7 +172,7 @@ actionMetaFrom1Arg.meta.remote; typedState = typedActionHandlerReducerMetaMap({ value: 0 }, actionMetaFrom1Arg); const typedActionWithMeta2TypedArgs: (value: number, remote: boolean) => ReduxActions.ActionMeta = - ReduxActions.createAction( + ReduxActions.createAction( 'INCREMENT_BY', (amount, remote) => ({ increase: amount }), (amount, remote) => ({ remote }) From e66cc7c8203508b64bca65b716c0d60c08566f67 Mon Sep 17 00:00:00 2001 From: Florian Richter Date: Tue, 12 Sep 2017 13:40:45 +0200 Subject: [PATCH 2/2] Set required tsc version for depended package --- types/redux-actions/index.d.ts | 10 ++++------ types/redux-promise/index.d.ts | 1 + 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/types/redux-actions/index.d.ts b/types/redux-actions/index.d.ts index d909fcfc7a..cc8e813e7c 100644 --- a/types/redux-actions/index.d.ts +++ b/types/redux-actions/index.d.ts @@ -41,23 +41,21 @@ export interface ReducerNextThrowMeta { throw?(state: State, action: ActionMeta): State; } -export type ActionFunctions = - ActionFunction> | - ActionFunctionAny>; +export type ActionFunctions = ActionFunctionAny>; export type Reducer = (state: State, action: Action) => State; export type ReducerMeta = (state: State, action: ActionMeta) => State; /** argument inferring borrowed from lodash definitions */ -export type ActionFunction = (t1?: T1, t2?: T2, t3?: T3, t4?: T4) => R; +export type ActionFunction = (t1?: T1, t2?: T2, t3?: T3, t4?: T4) => R; export type ActionFunctionAny = (...args: any[]) => R; export function createAction( actionType: string ): ActionFunction>; -export function createAction( +export function createAction( actionType: string, payloadCreator: ActionFunction ): ActionFunction, Arg1, Arg2, Arg3, Arg4>; @@ -102,7 +100,7 @@ export function handleActions( export function combineActions(...actionTypes: Array | string>): string; -export interface ActionMap { +export interface ActionMap { [actionType: string]: ActionMap | ActionFunctionAny | diff --git a/types/redux-promise/index.d.ts b/types/redux-promise/index.d.ts index 415709cd4e..5c0a5b3f50 100644 --- a/types/redux-promise/index.d.ts +++ b/types/redux-promise/index.d.ts @@ -2,6 +2,7 @@ // Project: https://github.com/acdlite/redux-promise // Definitions by: Rogelio Morrell Caballero , Kaur Kuut // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 import * as Redux from 'redux';