Skip to content

Commit

Permalink
convert combineReducers to typescript (reduxjs#3531)
Browse files Browse the repository at this point in the history
* convert combineReducers to typescript

* use idiomatic JS
  • Loading branch information
cellog authored and timdorr committed Aug 29, 2019
1 parent 5eb38ef commit 9f963b9
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions src/combineReducers.js → src/combineReducers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import {
AnyAction,
Action,
ReducersMapObject,
StateFromReducersMapObject
} from '..'
import ActionTypes from './utils/actionTypes'
import warning from './utils/warning'
import isPlainObject from './utils/isPlainObject'

function getUndefinedStateErrorMessage(key, action) {
function getUndefinedStateErrorMessage(key: string, action: Action) {
const actionType = action && action.type
const actionDescription =
(actionType && `action "${String(actionType)}"`) || 'an action'
Expand All @@ -15,10 +21,10 @@ function getUndefinedStateErrorMessage(key, action) {
}

function getUnexpectedStateShapeWarningMessage(
inputState,
reducers,
action,
unexpectedKeyCache
inputState: object,
reducers: ReducersMapObject,
action: Action,
unexpectedKeyCache: { [key: string]: true }
) {
const reducerKeys = Object.keys(reducers)
const argumentName =
Expand All @@ -34,9 +40,13 @@ function getUnexpectedStateShapeWarningMessage(
}

if (!isPlainObject(inputState)) {
const match = Object.prototype.toString
.call(inputState)
.match(/\s([a-z|A-Z]+)/)
const matchType = match ? match[1] : ''
return (
`The ${argumentName} has unexpected type of "` +
{}.toString.call(inputState).match(/\s([a-z|A-Z]+)/)[1] +
matchType +
`". Expected argument to be an object with the following ` +
`keys: "${reducerKeys.join('", "')}"`
)
Expand All @@ -62,7 +72,7 @@ function getUnexpectedStateShapeWarningMessage(
}
}

function assertReducerShape(reducers) {
function assertReducerShape(reducers: ReducersMapObject) {
Object.keys(reducers).forEach(key => {
const reducer = reducers[key]
const initialState = reducer(undefined, { type: ActionTypes.INIT })
Expand Down Expand Up @@ -110,9 +120,9 @@ function assertReducerShape(reducers) {
* @returns {Function} A reducer function that invokes every reducer inside the
* passed object, and builds a state object with the same shape.
*/
export default function combineReducers(reducers) {
export default function combineReducers(reducers: ReducersMapObject) {
const reducerKeys = Object.keys(reducers)
const finalReducers = {}
const finalReducers: ReducersMapObject = {}
for (let i = 0; i < reducerKeys.length; i++) {
const key = reducerKeys[i]

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

// This is used to make sure we don't warn about the same
// keys multiple times.
let unexpectedKeyCache
let unexpectedKeyCache: { [key: string]: true }
if (process.env.NODE_ENV !== 'production') {
unexpectedKeyCache = {}
}

let shapeAssertionError
let shapeAssertionError: Error
try {
assertReducerShape(finalReducers)
} catch (e) {
shapeAssertionError = e
}

return function combination(state = {}, action) {
return function combination(
state: StateFromReducersMapObject<typeof reducers> = {},
action: AnyAction
) {
if (shapeAssertionError) {
throw shapeAssertionError
}
Expand All @@ -160,7 +173,7 @@ export default function combineReducers(reducers) {
}

let hasChanged = false
const nextState = {}
const nextState: StateFromReducersMapObject<typeof reducers> = {}
for (let i = 0; i < finalReducerKeys.length; i++) {
const key = finalReducerKeys[i]
const reducer = finalReducers[key]
Expand Down

0 comments on commit 9f963b9

Please sign in to comment.