Skip to content

Commit 9f963b9

Browse files
cellogtimdorr
authored andcommitted
convert combineReducers to typescript (reduxjs#3531)
* convert combineReducers to typescript * use idiomatic JS
1 parent 5eb38ef commit 9f963b9

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

src/combineReducers.js renamed to src/combineReducers.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
import {
2+
AnyAction,
3+
Action,
4+
ReducersMapObject,
5+
StateFromReducersMapObject
6+
} from '..'
17
import ActionTypes from './utils/actionTypes'
28
import warning from './utils/warning'
39
import isPlainObject from './utils/isPlainObject'
410

5-
function getUndefinedStateErrorMessage(key, action) {
11+
function getUndefinedStateErrorMessage(key: string, action: Action) {
612
const actionType = action && action.type
713
const actionDescription =
814
(actionType && `action "${String(actionType)}"`) || 'an action'
@@ -15,10 +21,10 @@ function getUndefinedStateErrorMessage(key, action) {
1521
}
1622

1723
function getUnexpectedStateShapeWarningMessage(
18-
inputState,
19-
reducers,
20-
action,
21-
unexpectedKeyCache
24+
inputState: object,
25+
reducers: ReducersMapObject,
26+
action: Action,
27+
unexpectedKeyCache: { [key: string]: true }
2228
) {
2329
const reducerKeys = Object.keys(reducers)
2430
const argumentName =
@@ -34,9 +40,13 @@ function getUnexpectedStateShapeWarningMessage(
3440
}
3541

3642
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] : ''
3747
return (
3848
`The ${argumentName} has unexpected type of "` +
39-
{}.toString.call(inputState).match(/\s([a-z|A-Z]+)/)[1] +
49+
matchType +
4050
`". Expected argument to be an object with the following ` +
4151
`keys: "${reducerKeys.join('", "')}"`
4252
)
@@ -62,7 +72,7 @@ function getUnexpectedStateShapeWarningMessage(
6272
}
6373
}
6474

65-
function assertReducerShape(reducers) {
75+
function assertReducerShape(reducers: ReducersMapObject) {
6676
Object.keys(reducers).forEach(key => {
6777
const reducer = reducers[key]
6878
const initialState = reducer(undefined, { type: ActionTypes.INIT })
@@ -110,9 +120,9 @@ function assertReducerShape(reducers) {
110120
* @returns {Function} A reducer function that invokes every reducer inside the
111121
* passed object, and builds a state object with the same shape.
112122
*/
113-
export default function combineReducers(reducers) {
123+
export default function combineReducers(reducers: ReducersMapObject) {
114124
const reducerKeys = Object.keys(reducers)
115-
const finalReducers = {}
125+
const finalReducers: ReducersMapObject = {}
116126
for (let i = 0; i < reducerKeys.length; i++) {
117127
const key = reducerKeys[i]
118128

@@ -130,19 +140,22 @@ export default function combineReducers(reducers) {
130140

131141
// This is used to make sure we don't warn about the same
132142
// keys multiple times.
133-
let unexpectedKeyCache
143+
let unexpectedKeyCache: { [key: string]: true }
134144
if (process.env.NODE_ENV !== 'production') {
135145
unexpectedKeyCache = {}
136146
}
137147

138-
let shapeAssertionError
148+
let shapeAssertionError: Error
139149
try {
140150
assertReducerShape(finalReducers)
141151
} catch (e) {
142152
shapeAssertionError = e
143153
}
144154

145-
return function combination(state = {}, action) {
155+
return function combination(
156+
state: StateFromReducersMapObject<typeof reducers> = {},
157+
action: AnyAction
158+
) {
146159
if (shapeAssertionError) {
147160
throw shapeAssertionError
148161
}
@@ -160,7 +173,7 @@ export default function combineReducers(reducers) {
160173
}
161174

162175
let hasChanged = false
163-
const nextState = {}
176+
const nextState: StateFromReducersMapObject<typeof reducers> = {}
164177
for (let i = 0; i < finalReducerKeys.length; i++) {
165178
const key = finalReducerKeys[i]
166179
const reducer = finalReducers[key]

0 commit comments

Comments
 (0)