Skip to content

convert combineReducers to typescript #3531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 29, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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