You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// === Reproduction ===exporttypeActionReducer<State>=(state: State|undefined)=>State;exportfunctioncreateReducer<State>(initialState: State): ActionReducer<State>{return{}asany;}exportfunctioncreateFeature<State>(config: {reducer: ActionReducer<State>,selectors: (state: State)=>unknown,}){}createFeature({reducer: createReducer(''),// the `state` type is `unknown`// if we remove `state` argument the error will disappearselectors: (state)=>({}),// selectors: () => ({})});// === Workarounds ===// === Workaround 1:// Define return type as another genericexportfunctioncreateReducer2<State,ReducerextendsActionReducer<State>=ActionReducer<State>>(initialState: State): Reducer{return{}asany;}createFeature({reducer: createReducer2(123),// the `state` type is correctly inferred (`number` in this case)selectors: (state)=>({})});
More workarounds are available at the playground link.
π Actual behavior
A generic type is not correctly inferred from the function return type when it is a function expression type.
By the way, I noticed that the State type will be correctly inferred when the ActionReducer from the example above is not defined as a function expression type:
// `ActionReducer` is not a function type anymoreexporttypeActionReducer<T>=T|undefined;exportfunctioncreateReducer<T>(initialState: T): T{return{}asany;}exportfunctioncreateFeature<State>(config: {reducer: ActionReducer<State>,selectors: (state: State)=>unknown,}){}createFeature({reducer: createReducer(''),// the `state` type is correctly inferredselectors: (state)=>({}),});
π Expected behavior
I'd expect the same behavior when ActionReducer is typed as a function expression.
The text was updated successfully, but these errors were encountered:
Bug Report
π Version & Regression Information
Tested with different TS v4 versions. It does not seem like a regression.
β― Playground Link
Playground link with relevant code
π» Code
More workarounds are available at the playground link.
π Actual behavior
A generic type is not correctly inferred from the function return type when it is a function expression type.
By the way, I noticed that the
State
type will be correctly inferred when theActionReducer
from the example above is not defined as a function expression type:π Expected behavior
I'd expect the same behavior when
ActionReducer
is typed as a function expression.The text was updated successfully, but these errors were encountered: