From b4f231d0d9aceb30340671fecfd10a0738bf5836 Mon Sep 17 00:00:00 2001 From: Aleh Kashnikau Date: Thu, 5 Jan 2017 23:05:18 +0300 Subject: [PATCH] Mapped type for combineReducers in index.d.ts (#2182) * Add mapped type for combineReducers in index.d.ts Updated typescript to 2.1.4 Updated typescript-definition-tester to 0.0.5 Updated typescript tests to use proper import Added mapped type to index.d.ts * add strict null check for reducer Updated Reducer type in index.d.ts Add strictNullChecks flag to typescript spec --- index.d.ts | 8 ++++---- package.json | 4 ++-- test/typescript.spec.js | 3 +++ test/typescript/actionCreators.ts | 2 +- test/typescript/actions.ts | 2 +- test/typescript/compose.ts | 2 +- test/typescript/dispatch.ts | 4 ++-- test/typescript/middleware.ts | 8 ++++---- test/typescript/reducers.ts | 5 ++--- test/typescript/store.ts | 6 +++--- yarn.lock | 18 +++++++++++------- 11 files changed, 34 insertions(+), 28 deletions(-) diff --git a/index.d.ts b/index.d.ts index 88a4c0fa30..7c62f0df79 100644 --- a/index.d.ts +++ b/index.d.ts @@ -43,13 +43,13 @@ export interface Action { * * @template S State object type. */ -export type Reducer = (state: S, action: A) => S; +export type Reducer = (state: S | undefined, action: A) => S; /** * Object whose values correspond to different reducer functions. */ -export interface ReducersMapObject { - [key: string]: Reducer; +export type ReducersMapObject = { + [K in keyof S]: Reducer; } /** @@ -70,7 +70,7 @@ export interface ReducersMapObject { * @returns A reducer function that invokes every reducer inside the passed * object, and builds a state object with the same shape. */ -export function combineReducers(reducers: ReducersMapObject): Reducer; +export function combineReducers(reducers: ReducersMapObject): Reducer; /* store */ diff --git a/package.json b/package.json index 54e2e707e2..69cc14d51a 100644 --- a/package.json +++ b/package.json @@ -111,8 +111,8 @@ "jest": "^18.0.0", "rimraf": "^2.3.4", "rxjs": "^5.0.0-beta.6", - "typescript": "^1.8.0", - "typescript-definition-tester": "0.0.4", + "typescript": "^2.1.0", + "typescript-definition-tester": "0.0.5", "webpack": "^1.9.6" }, "npmName": "redux", diff --git a/test/typescript.spec.js b/test/typescript.spec.js index 8943a15658..de86902bb0 100644 --- a/test/typescript.spec.js +++ b/test/typescript.spec.js @@ -6,6 +6,9 @@ describe('TypeScript definitions', function () { tt.compileDirectory( __dirname + '/typescript', fileName => fileName.match(/\.ts$/), + { + strictNullChecks: true + }, () => done() ) }) diff --git a/test/typescript/actionCreators.ts b/test/typescript/actionCreators.ts index c3eac83b00..a2ec2c8f35 100644 --- a/test/typescript/actionCreators.ts +++ b/test/typescript/actionCreators.ts @@ -1,7 +1,7 @@ import { ActionCreator, Action, Dispatch, bindActionCreators, ActionCreatorsMapObject -} from "../../index.d.ts"; +} from "../../"; interface AddTodoAction extends Action { diff --git a/test/typescript/actions.ts b/test/typescript/actions.ts index 1a0bb29d03..1122c13357 100644 --- a/test/typescript/actions.ts +++ b/test/typescript/actions.ts @@ -1,4 +1,4 @@ -import {Action as ReduxAction} from "../../index.d.ts"; +import {Action as ReduxAction} from "../../"; namespace FSA { diff --git a/test/typescript/compose.ts b/test/typescript/compose.ts index 3fbb4d0dbc..3e3d665277 100644 --- a/test/typescript/compose.ts +++ b/test/typescript/compose.ts @@ -1,4 +1,4 @@ -import {compose} from "../../index.d.ts"; +import {compose} from "../../"; // copied from DefinitelyTyped/compose-function diff --git a/test/typescript/dispatch.ts b/test/typescript/dispatch.ts index bc54b17fb9..271135f802 100644 --- a/test/typescript/dispatch.ts +++ b/test/typescript/dispatch.ts @@ -1,4 +1,4 @@ -import {Dispatch, Action} from "../../index.d.ts"; +import {Dispatch, Action} from "../../"; declare const dispatch: Dispatch; @@ -6,7 +6,7 @@ declare const dispatch: Dispatch; const dispatchResult: Action = dispatch({type: 'TYPE'}); // thunk -declare module "../../index.d.ts" { +declare module "../../" { export interface Dispatch { (asyncAction: (dispatch: Dispatch, getState: () => S) => R): R; } diff --git a/test/typescript/middleware.ts b/test/typescript/middleware.ts index 5111873df9..1b3386841e 100644 --- a/test/typescript/middleware.ts +++ b/test/typescript/middleware.ts @@ -1,15 +1,15 @@ import { Middleware, MiddlewareAPI, applyMiddleware, createStore, Dispatch, Reducer, Action -} from "../../index.d.ts"; +} from "../../"; -declare module "../../index.d.ts" { +declare module "../../" { export interface Dispatch { (asyncAction: (dispatch: Dispatch, getState: () => S) => R): R; } } -type Thunk = (dispatch: Dispatch, getState: () => S) => O; +type Thunk = (dispatch: Dispatch, getState?: () => S) => O; const thunkMiddleware: Middleware = ({dispatch, getState}: MiddlewareAPI) => @@ -52,7 +52,7 @@ const storeWithThunkMiddleware = createStore( ); storeWithThunkMiddleware.dispatch( - (dispatch, getState) => { + (dispatch: Dispatch, getState: () => State) => { const todos: string[] = getState().todos; dispatch({type: 'ADD_TODO'}) } diff --git a/test/typescript/reducers.ts b/test/typescript/reducers.ts index 215b872349..9e49bce9c9 100644 --- a/test/typescript/reducers.ts +++ b/test/typescript/reducers.ts @@ -1,7 +1,7 @@ import { Reducer, Action, combineReducers, ReducersMapObject -} from "../../index.d.ts"; +} from "../../"; type TodosState = string[]; @@ -47,8 +47,7 @@ type RootState = { counter: CounterState; } - -const rootReducer: Reducer = combineReducers({ +const rootReducer: Reducer = combineReducers({ todos: todosReducer, counter: counterReducer, }) diff --git a/test/typescript/store.ts b/test/typescript/store.ts index 2939a237dd..0de19af192 100644 --- a/test/typescript/store.ts +++ b/test/typescript/store.ts @@ -1,7 +1,7 @@ import { Store, createStore, Reducer, Action, StoreEnhancer, GenericStoreEnhancer, StoreCreator, StoreEnhancerStoreCreator, Unsubscribe -} from "../../index.d.ts"; +} from "../../"; type State = { @@ -22,10 +22,10 @@ const storeWithPreloadedState: Store = createStore(reducer, { }); const genericEnhancer: GenericStoreEnhancer = (next: StoreEnhancerStoreCreator) => next; -const specificEnhencer: StoreEnhancer = next => next; +const specificEnhancer: StoreEnhancer = next => next; const storeWithGenericEnhancer: Store = createStore(reducer, genericEnhancer); -const storeWithSpecificEnhancer: Store = createStore(reducer, specificEnhencer); +const storeWithSpecificEnhancer: Store = createStore(reducer, specificEnhancer); const storeWithPreloadedStateAndEnhancer: Store = createStore(reducer, { todos: [] diff --git a/yarn.lock b/yarn.lock index abbf0a201f..e310a94394 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2719,7 +2719,11 @@ lodash.flatten@^4.4.0: version "4.4.0" resolved "https://registry.yarnpkg.com/lodash.flatten/-/lodash.flatten-4.4.0.tgz#f31c22225a9632d2bbf8e4addbef240aa765a61f" -lodash.isarguments@^3.0.0, lodash.isarguments@~3.0.7: +lodash.isarguments@^3.0.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/lodash.isarguments/-/lodash.isarguments-3.1.0.tgz#2f573d85c6a24289ff00663b491c1d338ff3458a" + +lodash.isarguments@~3.0.7: version "3.0.9" resolved "https://registry.yarnpkg.com/lodash.isarguments/-/lodash.isarguments-3.0.9.tgz#3c4994a4210f340d49ccfafa62176296207d8675" @@ -4310,17 +4314,17 @@ typedarray@^0.0.6: version "0.0.6" resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" -typescript-definition-tester@0.0.4: - version "0.0.4" - resolved "https://registry.yarnpkg.com/typescript-definition-tester/-/typescript-definition-tester-0.0.4.tgz#94b9edc4fe803b47f5f64ff5ddaf8eed1196156c" +typescript-definition-tester@0.0.5: + version "0.0.5" + resolved "https://registry.yarnpkg.com/typescript-definition-tester/-/typescript-definition-tester-0.0.5.tgz#91c574d78ea05b81ed81244d50ec30d8240c356f" dependencies: assertion-error "^1.0.1" dts-bundle "^0.2.0" lodash "^3.6.0" -typescript@^1.8.0: - version "1.8.10" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-1.8.10.tgz#b475d6e0dff0bf50f296e5ca6ef9fbb5c7320f1e" +typescript@^2.1.0: + version "2.1.4" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.1.4.tgz#b53b69fb841126acb1dd4b397d21daba87572251" uglify-js@^2.6, uglify-js@~2.7.3: version "2.7.5"