Skip to content
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

Revert "Infer action types from combineReducers" #3467

Merged
merged 1 commit into from
Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 6 additions & 7 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,12 @@ export type ReducersMapObject<S = any, A extends Action = Action> = {
* @returns A reducer function that invokes every reducer inside the passed
* object, and builds a state object with the same shape.
*/
export function combineReducers<T extends ReducersMapObject<any, any>>(
reducers: T
): Reducer<InferStateType<T>, InferActionTypes<InferReducerTypes<T>>>

type InferActionTypes<R> = R extends Reducer<any, infer A> ? A : AnyAction
type InferReducerTypes<T> = T extends Record<any, infer R> ? R : Reducer
type InferStateType<T> = T extends ReducersMapObject<infer S, any> ? S : never
export function combineReducers<S>(
reducers: ReducersMapObject<S, any>
): Reducer<S>
export function combineReducers<S, A extends Action = AnyAction>(
reducers: ReducersMapObject<S, A>
): Reducer<S, A>

/* store */

Expand Down
21 changes: 10 additions & 11 deletions test/typescript/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function simple() {
// Combined reducer also accepts any action.
const combined = combineReducers({ sub: reducer })

let cs = combined(undefined, { type: 'init' })
let cs: { sub: State } = combined(undefined, { type: 'init' })
cs = combined(cs, { type: 'INCREMENT', count: 10 })

// Combined reducer's state is strictly checked.
Expand Down Expand Up @@ -110,18 +110,17 @@ function discriminated() {
// typings:expect-error
s = reducer(s, { type: 'SOME_OTHER_TYPE', someField: 'value' })

// Combined reducer accepts a union actions types accepted each reducer,
// which can be very permissive for unknown third-party reducers.
const combined = combineReducers({
sub: reducer,
unknown: (state => state) as Reducer
})
// Combined reducer accepts any action by default which allows to include
// third-party reducers without the need to add their actions to the union.
const combined = combineReducers({ sub: reducer })

let cs = combined(undefined, { type: 'init' })
cs = combined(cs, { type: 'SOME_OTHER_TYPE', someField: 'value' })
let cs: { sub: State } = combined(undefined, { type: 'init' })
cs = combined(cs, { type: 'SOME_OTHER_TYPE' })

// Combined reducer can be made to only accept known actions.
const strictCombined = combineReducers({ sub: reducer })
const strictCombined = combineReducers<{ sub: State }, MyAction>({
sub: reducer
})

strictCombined(cs, { type: 'INCREMENT' })
// typings:expect-error
Expand Down Expand Up @@ -180,7 +179,7 @@ function typeGuards() {

const combined = combineReducers({ sub: reducer })

let cs = combined(undefined, { type: 'init' })
let cs: { sub: State } = combined(undefined, { type: 'init' })
cs = combined(cs, { type: 'INCREMENT', count: 10 })
}

Expand Down