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
typeAction<Textendsstring,P>={readonlytype: T;payload: P;}typeActionCreator<Textendsstring,P>={readonlytype: T,(payload: P): Action<T,P>};typeReducer<S,Textendsstring,P>=(state: S,action: Action<T,P>)=>S;typeReducerFor<S,AC>=ACextendsActionCreator<infer T, infer P> ? Reducer<S,T,P> : never;declarefunctionhandleAction<AC>(action: AC,reducer: ReducerFor<{value: string},AC>): void;declareconstSomeAction: ActionCreator<"TestAction",string>;handleAction(SomeAction,(state,action)=>({ ...state,value: action.payload}));/* ^ resolved as Action<string, {}> ^ error appears here:Type '{}' is not assignable to type 'string'.ts(2322)The expected type comes from property 'value' which is declared here on type '{ value: string; }'*/
As indicated in the comments, action is being resolved as Action<string, {}> instead of Action<"TestAction", string>.
However, if I write the conditional inference in a different way, then this will work:
typeReducerFor<S,AC>=Reducer<S,ACextendsActionCreator<infer T,any> ? T : never,ACextendsActionCreator<any, infer P> ? P : never>;declareconstSomeAction: ActionCreator<"TestAction",string>;handleActionTest(SomeAction,(state,action)=>({ ...state,value: action.payload}));// ^ correctly resolved as Action<"TestAction", string>
Expected behavior:
The compiler should correctly handle the type inference and return a type of Action<"TestAction", string>
Actual behavior:
Incorrect type inference to Action<string, {}>
typeInnerBox<T>={value: T;}typeOuterBox<T>={inner: InnerBox<T>};typeBoxConsumerFromOuterBox<TProducer>=TProducerextendsOuterBox<infer TContent> ?
(box: InnerBox<TContent>)=>void :
never;declarefunctionpassContentsToFunc<T>(outerBox: T,consumer: BoxConsumerFromOuterBox<T>): void;declareconstOuterBoxOfString: OuterBox<string>;passContentsToFunc(OuterBoxOfString,box=>{// Error, but should be OK// Note: LS shows desired types in many locations!constp: string=box.value;});
TypeScript Version: 3.4.0-dev.20190312
Code
The following code throws the indicated error:
As indicated in the comments,
action
is being resolved asAction<string, {}>
instead ofAction<"TestAction", string>
.However, if I write the conditional inference in a different way, then this will work:
Expected behavior:
The compiler should correctly handle the type inference and return a type of
Action<"TestAction", string>
Actual behavior:
Incorrect type inference to
Action<string, {}>
Playground Link:
Link with issue
Link with workaround
The text was updated successfully, but these errors were encountered: