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

Incorrect type inference when conditionally inferring multiple types #30341

Closed
ylibrach opened this issue Mar 12, 2019 · 1 comment
Closed

Incorrect type inference when conditionally inferring multiple types #30341

ylibrach opened this issue Mar 12, 2019 · 1 comment
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@ylibrach
Copy link

TypeScript Version: 3.4.0-dev.20190312

Code

The following code throws the indicated error:

type Action<T extends string, P> = { readonly type: T; payload: P; }
type ActionCreator<T extends string, P> = { readonly type: T, (payload: P): Action<T, P> };
type Reducer<S, T extends string, P> = (state: S, action: Action<T, P>) => S;
type ReducerFor<S, AC> = AC extends ActionCreator<infer T, infer P> ? Reducer<S, T, P> : never;

declare function handleAction<AC>(action: AC, reducer: ReducerFor<{ value: string }, AC>): void;
declare const SomeAction: 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:

type ReducerFor<S, AC> = 
   Reducer<S, AC extends ActionCreator<infer T, any> ? T : never, 
              AC extends ActionCreator<any, infer P> ? P : never>;

declare const SomeAction: 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, {}>

Playground Link:
Link with issue

Link with workaround

@RyanCavanaugh
Copy link
Member

The preferred signature would be

declare function handleAction<Q extends string, U>(action: ActionCreator<Q, U>, reducer: ReducerFor<{ value: string }, ActionCreator<Q, U>>): void;

which works as expected and will give you better errors when incorrectly invoked

@ahejlsberg here's a simplified repro

type InnerBox<T> = {
  value: T;
}

type OuterBox<T> = {
  inner: InnerBox<T>
};

type BoxConsumerFromOuterBox<TProducer> =
  TProducer extends OuterBox<infer TContent> ?
      (box: InnerBox<TContent>) => void :
      never;

declare function passContentsToFunc<T>(outerBox: T, consumer: BoxConsumerFromOuterBox<T>): void;

declare const OuterBoxOfString: OuterBox<string>;
passContentsToFunc(OuterBoxOfString, box => {
  // Error, but should be OK
  // Note: LS shows desired types in many locations!
  const p: string = box.value;
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants