1
+ import {
2
+ AnyAction ,
3
+ Action ,
4
+ ReducersMapObject ,
5
+ StateFromReducersMapObject
6
+ } from '..'
1
7
import ActionTypes from './utils/actionTypes'
2
8
import warning from './utils/warning'
3
9
import isPlainObject from './utils/isPlainObject'
4
10
5
- function getUndefinedStateErrorMessage ( key , action ) {
11
+ function getUndefinedStateErrorMessage ( key : string , action : Action ) {
6
12
const actionType = action && action . type
7
13
const actionDescription =
8
14
( actionType && `action "${ String ( actionType ) } "` ) || 'an action'
@@ -15,10 +21,10 @@ function getUndefinedStateErrorMessage(key, action) {
15
21
}
16
22
17
23
function getUnexpectedStateShapeWarningMessage (
18
- inputState ,
19
- reducers ,
20
- action ,
21
- unexpectedKeyCache
24
+ inputState : object ,
25
+ reducers : ReducersMapObject ,
26
+ action : Action ,
27
+ unexpectedKeyCache : { [ key : string ] : true }
22
28
) {
23
29
const reducerKeys = Object . keys ( reducers )
24
30
const argumentName =
@@ -36,7 +42,7 @@ function getUnexpectedStateShapeWarningMessage(
36
42
if ( ! isPlainObject ( inputState ) ) {
37
43
return (
38
44
`The ${ argumentName } has unexpected type of "` +
39
- { } . toString . call ( inputState ) . match ( / \s ( [ a - z | A - Z ] + ) / ) [ 1 ] +
45
+ ( { } as any ) . toString . call ( inputState ) . match ( / \s ( [ a - z | A - Z ] + ) / ) [ 1 ] +
40
46
`". Expected argument to be an object with the following ` +
41
47
`keys: "${ reducerKeys . join ( '", "' ) } "`
42
48
)
@@ -62,7 +68,7 @@ function getUnexpectedStateShapeWarningMessage(
62
68
}
63
69
}
64
70
65
- function assertReducerShape ( reducers ) {
71
+ function assertReducerShape ( reducers : ReducersMapObject ) {
66
72
Object . keys ( reducers ) . forEach ( key => {
67
73
const reducer = reducers [ key ]
68
74
const initialState = reducer ( undefined , { type : ActionTypes . INIT } )
@@ -110,9 +116,9 @@ function assertReducerShape(reducers) {
110
116
* @returns {Function } A reducer function that invokes every reducer inside the
111
117
* passed object, and builds a state object with the same shape.
112
118
*/
113
- export default function combineReducers ( reducers ) {
119
+ export default function combineReducers ( reducers : ReducersMapObject ) {
114
120
const reducerKeys = Object . keys ( reducers )
115
- const finalReducers = { }
121
+ const finalReducers : ReducersMapObject = { }
116
122
for ( let i = 0 ; i < reducerKeys . length ; i ++ ) {
117
123
const key = reducerKeys [ i ]
118
124
@@ -130,19 +136,22 @@ export default function combineReducers(reducers) {
130
136
131
137
// This is used to make sure we don't warn about the same
132
138
// keys multiple times.
133
- let unexpectedKeyCache
139
+ let unexpectedKeyCache : { [ key : string ] : true }
134
140
if ( process . env . NODE_ENV !== 'production' ) {
135
141
unexpectedKeyCache = { }
136
142
}
137
143
138
- let shapeAssertionError
144
+ let shapeAssertionError : Error
139
145
try {
140
146
assertReducerShape ( finalReducers )
141
147
} catch ( e ) {
142
148
shapeAssertionError = e
143
149
}
144
150
145
- return function combination ( state = { } , action ) {
151
+ return function combination (
152
+ state : StateFromReducersMapObject < typeof reducers > = { } ,
153
+ action : AnyAction
154
+ ) {
146
155
if ( shapeAssertionError ) {
147
156
throw shapeAssertionError
148
157
}
@@ -160,7 +169,7 @@ export default function combineReducers(reducers) {
160
169
}
161
170
162
171
let hasChanged = false
163
- const nextState = { }
172
+ const nextState : StateFromReducersMapObject < typeof reducers > = { }
164
173
for ( let i = 0 ; i < finalReducerKeys . length ; i ++ ) {
165
174
const key = finalReducerKeys [ i ]
166
175
const reducer = finalReducers [ key ]
0 commit comments