Skip to content

Conversation

@drewnoakes
Copy link

This PR flows names and types of state/reducers through the combineReducers function.

Using TypeScript's Mapped Types, validate the reducer map object contains all required properties, and that the child reducers return the correct types.

For example:

interface RootType {
  users: IUser[],
  sessions: ISession[];
}

const users = (state: IUser[]) => state; // NOOP
const sessions = (state: ISession[]) => state; // NOOP

const reducer = combineReducers<RootType>({ users, sessions });

That worked before, as it does with this PR.

With this change, various errors are caught at compile time:

  • Mismatched child reducer name vs. RootType property name
  • Missing child reducers
  • Child reducers that return different types

Current user code that doesn't specify the type on the call to combineReducers has the type inferred from the reducer map, so this change doesn't limit them, although the returned reducer's type reflects the reality of the data it will produce.

This commit flows names and types of state/reducers through
the combineReducers function.

Using TypeScript's Mapped Types, validate the reducer map object
contains all required properties, and that the child reducers return the
correct types.

For example:

    interface RootType {
      users: IUser[],
      sessions: ISession[];
    }

    const users = (state: IUser[]) => state; // NOOP
    const sessions = (state: ISession[]) => state; // NOOP

    const reducer = combineReducers<RootType>({ users, sessions });

That worked before, as it does with this commit.

With this change, various errors are caught at compile time:

- Mismatched child reducer name vs. RootType property name
- Missing child reducers
- Child reducers that return different types

Current user code that doesn't specify the type on the call to
combineReducers has the type inferred from the reducer map, so this
change doesn't limit them, although the returned reducer's type
reflects the reality of the data it will produce.
@aikoven
Copy link
Collaborator

aikoven commented Jan 17, 2017

Already merged into the next branch: #2182

@drewnoakes
Copy link
Author

Thanks! Looking forward to being able to use it.

@drewnoakes drewnoakes closed this Jan 17, 2017
@drewnoakes drewnoakes deleted the feature/combine-reducers-typing branch January 17, 2017 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants