Skip to content

3.4 regression when using Redux nested combineReducers #30685

@OliverJAsh

Description

@OliverJAsh

TypeScript Version: 3.4.1

Search Terms:

Code

//
// Redux types
//

// import { combineReducers, Reducer } from 'redux';

type Reducer<S> = (state: S) => S;
declare function combineReducers<S>(reducers: { [K in keyof S]: Reducer<S[K]> }): Reducer<S>;

//
// Begin example
//

type MyState = { combined: { foo: number } };
declare const foo: Reducer<MyState['combined']['foo']>;

// When `strictFunctionTypes` is disabled…

{
    // Unexpected type error:
    // Property 'foo' is missing in type '{}' but required in type '{ foo: number; }'.
    const myReducer: Reducer<MyState> = combineReducers({
        combined: combineReducers({ foo }),
    });
}

{
    // Expected type: Reducer<{ combined: {}; }, AnyAction>
    // Actual type: Reducer<MyState, AnyAction>
    const myReducer = combineReducers({
        combined: combineReducers({ foo }),
    });
}

//
// Workaround:
//

{
    const combined = combineReducers({ foo });
    // No type error
    const myReducer: Reducer<MyState> = combineReducers({
        combined,
    });
}

{
    const combined = combineReducers({ foo });
    // Correct type inference
    const myReducer = combineReducers({
        combined,
    });
}

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions