Skip to content

Commit a5c1f46

Browse files
mkushertomipaul
authored andcommitted
Mapped type for combineReducers in index.d.ts (reduxjs#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 2fa37a0 commit a5c1f46

11 files changed

+4420
-20
lines changed

index.d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ export type Reducer<S> = (state: S, action: AnyAction) => S;
5858
/**
5959
* Object whose values correspond to different reducer functions.
6060
*/
61-
export interface ReducersMapObject {
62-
[key: string]: Reducer<any>;
61+
export type ReducersMapObject<S> = {
62+
[K in keyof S]: Reducer<S[K]>;
6363
}
6464

6565
/**
@@ -80,7 +80,7 @@ export interface ReducersMapObject {
8080
* @returns A reducer function that invokes every reducer inside the passed
8181
* object, and builds a state object with the same shape.
8282
*/
83-
export function combineReducers<S>(reducers: ReducersMapObject): Reducer<S>;
83+
export function combineReducers<S>(reducers: ReducersMapObject<S>): Reducer<S>;
8484

8585

8686
/* store */

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@
114114
"rollup-plugin-replace": "^2.0.0",
115115
"rollup-plugin-uglify": "^2.0.1",
116116
"rxjs": "^5.5.0",
117-
"typescript": "^1.8.0",
118-
"typescript-definition-tester": "0.0.4"
117+
"typescript": "^2.1.0",
118+
"typescript-definition-tester": "0.0.5"
119119
},
120120
"npmName": "redux",
121121
"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: []

0 commit comments

Comments
 (0)