Skip to content

Commit 8033325

Browse files
mkushertimdorr
authored andcommitted
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<S> type in index.d.ts Add strictNullChecks flag to typescript spec
1 parent 5c60ef7 commit 8033325

11 files changed

+34
-28
lines changed

index.d.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ export interface Action {
4343
*
4444
* @template S State object type.
4545
*/
46-
export type Reducer<S> = <A extends Action>(state: S, action: A) => S;
46+
export type Reducer<S> = <A extends Action>(state: S | undefined, action: A) => S;
4747

4848
/**
4949
* Object whose values correspond to different reducer functions.
5050
*/
51-
export interface ReducersMapObject {
52-
[key: string]: Reducer<any>;
51+
export type ReducersMapObject<S> = {
52+
[K in keyof S]: Reducer<S[K]>;
5353
}
5454

5555
/**
@@ -70,7 +70,7 @@ export interface ReducersMapObject {
7070
* @returns A reducer function that invokes every reducer inside the passed
7171
* object, and builds a state object with the same shape.
7272
*/
73-
export function combineReducers<S>(reducers: ReducersMapObject): Reducer<S>;
73+
export function combineReducers<S>(reducers: ReducersMapObject<S>): Reducer<S>;
7474

7575

7676
/* store */

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@
111111
"rollup-plugin-replace": "^1.1.1",
112112
"rollup-plugin-uglify": "^1.0.1",
113113
"rxjs": "^5.0.0-beta.6",
114-
"typescript": "^1.8.0",
115-
"typescript-definition-tester": "0.0.4"
114+
"typescript": "^2.1.0",
115+
"typescript-definition-tester": "0.0.5"
116116
},
117117
"npmName": "redux",
118118
"npmFileMap": [

test/typescript.spec.js

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ describe('TypeScript definitions', function () {
66
tt.compileDirectory(
77
__dirname + '/typescript',
88
fileName => fileName.match(/\.ts$/),
9+
{
10+
strictNullChecks: true
11+
},
912
() => done()
1013
)
1114
})

test/typescript/actionCreators.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
ActionCreator, Action, Dispatch,
33
bindActionCreators, ActionCreatorsMapObject
4-
} from "../../index";
4+
} from "../../"
55

66

77
interface AddTodoAction extends Action {

test/typescript/actions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Action as ReduxAction} from "../../index";
1+
import {Action as ReduxAction} from "../../"
22

33

44
namespace FSA {

test/typescript/compose.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {compose} from "../../index";
1+
import {compose} from "../../"
22

33
// copied from DefinitelyTyped/compose-function
44

test/typescript/dispatch.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import {Dispatch, Action} from "../../index";
1+
import {Dispatch, Action} from "../../"
22

33

44
declare const dispatch: Dispatch<any>;
55

66
const dispatchResult: Action = dispatch({type: 'TYPE'});
77

88
// thunk
9-
declare module "../../index" {
9+
declare module "../../" {
1010
export interface Dispatch<S> {
1111
<R>(asyncAction: (dispatch: Dispatch<S>, getState: () => S) => R): R;
1212
}

test/typescript/middleware.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import {
22
Middleware, MiddlewareAPI,
33
applyMiddleware, createStore, Dispatch, Reducer, Action
4-
} from "../../index";
4+
} from "../../"
55

6-
declare module "../../index" {
6+
declare module "../../" {
77
export interface Dispatch<S> {
88
<R>(asyncAction: (dispatch: Dispatch<S>, getState: () => S) => R): R;
99
}
1010
}
1111

12-
type Thunk<S, O> = (dispatch: Dispatch<S>, getState: () => S) => O;
12+
type Thunk<S, O> = (dispatch: Dispatch<S>, getState?: () => S) => O;
1313

1414
const thunkMiddleware: Middleware =
1515
<S>({dispatch, getState}: MiddlewareAPI<S>) =>
@@ -51,7 +51,7 @@ const storeWithThunkMiddleware = createStore(
5151
);
5252

5353
storeWithThunkMiddleware.dispatch(
54-
(dispatch, getState) => {
54+
(dispatch: Dispatch<State>, getState: () => State) => {
5555
const todos: string[] = getState().todos;
5656
dispatch({type: 'ADD_TODO'})
5757
}

test/typescript/reducers.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
Reducer, Action, combineReducers,
33
ReducersMapObject
4-
} from "../../index";
4+
} from "../../"
55

66

77
type TodosState = string[];
@@ -47,8 +47,7 @@ type RootState = {
4747
counter: CounterState;
4848
}
4949

50-
51-
const rootReducer: Reducer<RootState> = combineReducers<RootState>({
50+
const rootReducer: Reducer<RootState> = combineReducers({
5251
todos: todosReducer,
5352
counter: counterReducer,
5453
})

test/typescript/store.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
Store, createStore, Reducer, Action, StoreEnhancer, GenericStoreEnhancer,
33
StoreCreator, StoreEnhancerStoreCreator, Unsubscribe
4-
} from "../../index";
4+
} from "../../"
55

66

77
type State = {
@@ -22,10 +22,10 @@ const storeWithPreloadedState: Store<State> = createStore(reducer, {
2222
});
2323

2424
const genericEnhancer: GenericStoreEnhancer = <S>(next: StoreEnhancerStoreCreator<S>) => next;
25-
const specificEnhencer: StoreEnhancer<State> = next => next;
25+
const specificEnhancer: StoreEnhancer<State> = next => next;
2626

2727
const storeWithGenericEnhancer: Store<State> = createStore(reducer, genericEnhancer);
28-
const storeWithSpecificEnhancer: Store<State> = createStore(reducer, specificEnhencer);
28+
const storeWithSpecificEnhancer: Store<State> = createStore(reducer, specificEnhancer);
2929

3030
const storeWithPreloadedStateAndEnhancer: Store<State> = createStore(reducer, {
3131
todos: []

yarn.lock

+11-7
Original file line numberDiff line numberDiff line change
@@ -2610,7 +2610,11 @@ lodash.flatten@^4.4.0:
26102610
version "4.4.0"
26112611
resolved "https://registry.yarnpkg.com/lodash.flatten/-/lodash.flatten-4.4.0.tgz#f31c22225a9632d2bbf8e4addbef240aa765a61f"
26122612

2613-
lodash.isarguments@^3.0.0, lodash.isarguments@~3.0.7:
2613+
lodash.isarguments@^3.0.0:
2614+
version "3.1.0"
2615+
resolved "https://registry.yarnpkg.com/lodash.isarguments/-/lodash.isarguments-3.1.0.tgz#2f573d85c6a24289ff00663b491c1d338ff3458a"
2616+
2617+
lodash.isarguments@~3.0.7:
26142618
version "3.0.9"
26152619
resolved "https://registry.yarnpkg.com/lodash.isarguments/-/lodash.isarguments-3.0.9.tgz#3c4994a4210f340d49ccfafa62176296207d8675"
26162620

@@ -4143,17 +4147,17 @@ typedarray@^0.0.6:
41434147
version "0.0.6"
41444148
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
41454149

4146-
4147-
version "0.0.4"
4148-
resolved "https://registry.yarnpkg.com/typescript-definition-tester/-/typescript-definition-tester-0.0.4.tgz#94b9edc4fe803b47f5f64ff5ddaf8eed1196156c"
4150+
4151+
version "0.0.5"
4152+
resolved "https://registry.yarnpkg.com/typescript-definition-tester/-/typescript-definition-tester-0.0.5.tgz#91c574d78ea05b81ed81244d50ec30d8240c356f"
41494153
dependencies:
41504154
assertion-error "^1.0.1"
41514155
dts-bundle "^0.2.0"
41524156
lodash "^3.6.0"
41534157

4154-
typescript@^1.8.0:
4155-
version "1.8.10"
4156-
resolved "https://registry.yarnpkg.com/typescript/-/typescript-1.8.10.tgz#b475d6e0dff0bf50f296e5ca6ef9fbb5c7320f1e"
4158+
typescript@^2.1.0:
4159+
version "2.1.4"
4160+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.1.4.tgz#b53b69fb841126acb1dd4b397d21daba87572251"
41574161

41584162
uglify-js@^2.6, uglify-js@^2.6.1:
41594163
version "2.7.5"

0 commit comments

Comments
 (0)