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 =
@@ -34,9 +40,13 @@ function getUnexpectedStateShapeWarningMessage(
34
40
}
35
41
36
42
if ( ! isPlainObject ( inputState ) ) {
43
+ const match = Object . prototype . toString
44
+ . call ( inputState )
45
+ . match ( / \s ( [ a - z | A - Z ] + ) / )
46
+ const matchType = match ? match [ 1 ] : ''
37
47
return (
38
48
`The ${ argumentName } has unexpected type of "` +
39
- { } . toString . call ( inputState ) . match ( / \s ( [ a - z | A - Z ] + ) / ) [ 1 ] +
49
+ matchType +
40
50
`". Expected argument to be an object with the following ` +
41
51
`keys: "${ reducerKeys . join ( '", "' ) } "`
42
52
)
@@ -62,7 +72,7 @@ function getUnexpectedStateShapeWarningMessage(
62
72
}
63
73
}
64
74
65
- function assertReducerShape ( reducers ) {
75
+ function assertReducerShape ( reducers : ReducersMapObject ) {
66
76
Object . keys ( reducers ) . forEach ( key => {
67
77
const reducer = reducers [ key ]
68
78
const initialState = reducer ( undefined , { type : ActionTypes . INIT } )
@@ -110,9 +120,9 @@ function assertReducerShape(reducers) {
110
120
* @returns {Function } A reducer function that invokes every reducer inside the
111
121
* passed object, and builds a state object with the same shape.
112
122
*/
113
- export default function combineReducers ( reducers ) {
123
+ export default function combineReducers ( reducers : ReducersMapObject ) {
114
124
const reducerKeys = Object . keys ( reducers )
115
- const finalReducers = { }
125
+ const finalReducers : ReducersMapObject = { }
116
126
for ( let i = 0 ; i < reducerKeys . length ; i ++ ) {
117
127
const key = reducerKeys [ i ]
118
128
@@ -130,19 +140,22 @@ export default function combineReducers(reducers) {
130
140
131
141
// This is used to make sure we don't warn about the same
132
142
// keys multiple times.
133
- let unexpectedKeyCache
143
+ let unexpectedKeyCache : { [ key : string ] : true }
134
144
if ( process . env . NODE_ENV !== 'production' ) {
135
145
unexpectedKeyCache = { }
136
146
}
137
147
138
- let shapeAssertionError
148
+ let shapeAssertionError : Error
139
149
try {
140
150
assertReducerShape ( finalReducers )
141
151
} catch ( e ) {
142
152
shapeAssertionError = e
143
153
}
144
154
145
- return function combination ( state = { } , action ) {
155
+ return function combination (
156
+ state : StateFromReducersMapObject < typeof reducers > = { } ,
157
+ action : AnyAction
158
+ ) {
146
159
if ( shapeAssertionError ) {
147
160
throw shapeAssertionError
148
161
}
@@ -160,7 +173,7 @@ export default function combineReducers(reducers) {
160
173
}
161
174
162
175
let hasChanged = false
163
- const nextState = { }
176
+ const nextState : StateFromReducersMapObject < typeof reducers > = { }
164
177
for ( let i = 0 ; i < finalReducerKeys . length ; i ++ ) {
165
178
const key = finalReducerKeys [ i ]
166
179
const reducer = finalReducers [ key ]
0 commit comments