Skip to content

Commit 4b10792

Browse files
committed
convert combineReducers to typescript
1 parent 4f7ff64 commit 4b10792

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

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

Lines changed: 22 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 =
@@ -36,7 +42,7 @@ function getUnexpectedStateShapeWarningMessage(
3642
if (!isPlainObject(inputState)) {
3743
return (
3844
`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] +
4046
`". Expected argument to be an object with the following ` +
4147
`keys: "${reducerKeys.join('", "')}"`
4248
)
@@ -62,7 +68,7 @@ function getUnexpectedStateShapeWarningMessage(
6268
}
6369
}
6470

65-
function assertReducerShape(reducers) {
71+
function assertReducerShape(reducers: ReducersMapObject) {
6672
Object.keys(reducers).forEach(key => {
6773
const reducer = reducers[key]
6874
const initialState = reducer(undefined, { type: ActionTypes.INIT })
@@ -110,9 +116,9 @@ function assertReducerShape(reducers) {
110116
* @returns {Function} A reducer function that invokes every reducer inside the
111117
* passed object, and builds a state object with the same shape.
112118
*/
113-
export default function combineReducers(reducers) {
119+
export default function combineReducers(reducers: ReducersMapObject) {
114120
const reducerKeys = Object.keys(reducers)
115-
const finalReducers = {}
121+
const finalReducers: ReducersMapObject = {}
116122
for (let i = 0; i < reducerKeys.length; i++) {
117123
const key = reducerKeys[i]
118124

@@ -130,19 +136,22 @@ export default function combineReducers(reducers) {
130136

131137
// This is used to make sure we don't warn about the same
132138
// keys multiple times.
133-
let unexpectedKeyCache
139+
let unexpectedKeyCache: { [key: string]: true }
134140
if (process.env.NODE_ENV !== 'production') {
135141
unexpectedKeyCache = {}
136142
}
137143

138-
let shapeAssertionError
144+
let shapeAssertionError: Error
139145
try {
140146
assertReducerShape(finalReducers)
141147
} catch (e) {
142148
shapeAssertionError = e
143149
}
144150

145-
return function combination(state = {}, action) {
151+
return function combination(
152+
state: StateFromReducersMapObject<typeof reducers> = {},
153+
action: AnyAction
154+
) {
146155
if (shapeAssertionError) {
147156
throw shapeAssertionError
148157
}
@@ -160,7 +169,7 @@ export default function combineReducers(reducers) {
160169
}
161170

162171
let hasChanged = false
163-
const nextState = {}
172+
const nextState: StateFromReducersMapObject<typeof reducers> = {}
164173
for (let i = 0; i < finalReducerKeys.length; i++) {
165174
const key = finalReducerKeys[i]
166175
const reducer = finalReducers[key]

0 commit comments

Comments
 (0)