Closed
Description
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,
});
}