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

Conditional types infer gets wrong type if it inferred from union type #37734

Closed
ktsn opened this issue Apr 1, 2020 · 3 comments
Closed

Conditional types infer gets wrong type if it inferred from union type #37734

ktsn opened this issue Apr 1, 2020 · 3 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@ktsn
Copy link

ktsn commented Apr 1, 2020

TypeScript Version: 3.9.0-dev.20200330

Search Terms: infer function union conditional types

Code

interface A {
  test: never
}

type PropType<T> = 
  | { new(...args: any[]): T & object }
  | { new(...args: string[]): Function }

type ExtractProp<P extends PropType<any>> = P extends PropType<infer T> ? T : never

type Res = ExtractProp<PropType<A>> // Function | A

Expected behavior:
Res is of type A

Actual behavior:
Res is of type Function | A

Playground Link:
https://www.typescriptlang.org/play/?ssl=12&ssc=1&pln=1&pc=1#code/JYOwLgpgTgZghgYwgAgILIN4FgBQzmQDOYAXMiBAG7S4C+uuYAngA4oAKUA9iwCqsQAPLwB8yALzJc+AD6ZyEAO4AKAHTq4UAOaEycEEwDaAXQCUZXsgBkyLgCMAVhARhk9PMjkYFK9as06ZMRQoFom5sgAYgCuIC7AXCBuDDjMbMgAogAeYFCIYJw8guzIEDkQIAAmhMiFfAKC+kwiYpIlZZBVNXX8bIKgMNDIosgA-MPIZBTUUClpKABKEDWS2bn5dcXc9X2oLcgA9AdRsfGJnmi4QA

Related Issues:
vuejs/composition-api#291

Background:
In Vue.js, we annotate props by using built in PropType. Since PropType accepts any constructors, we have all possible cases in it as union type. (e.g. new(...args: any[]): T & object is for user defined class constructor, new(...args: string[]): Function is for Function constructor)

To use the props type, we need to extract actual type from PropType generics (ExtractProp in reproduction). We want to extract type A if the constructor type is of PropType<A>. But the actual behavior is Function | A.

I also found if I pass PropType via another object type, it infers correctly.
https://www.typescriptlang.org/play/#code/JYOwLgpgTgZghgYwgAgILIN4FgBQzmQDOYAXMiBAG7S4C+uuYAngA4oAKUA9iwCqsQAPLwB8yALzJc+AD6ZyEAO4AKAHTq4UAOaEycEEwDaAXQCUZXsgBkyLgCMAVhARhk9PMjkYFK9as06ZMRQoFom5sgAYgCuIC7AXCBuDDjMbMgAogAeYFCIYJw8guxikuzIEDkQIAAmhPJpEGSFfAKCoDDQyKJuyAD83chkFNRQKY3IAEoQ9ZLZufktgt6NzdytbIKoYrRiAPR7aLhAA

@RyanCavanaugh
Copy link
Member

It sounds like you don't want this conditional type to be distributive, so you should write

type ExtractProp<P extends PropType<any>> = [P] extends [PropType<infer T>] ? T : never

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Apr 1, 2020
@ktsn
Copy link
Author

ktsn commented Apr 2, 2020

Ah, I forgot about distribution of conditional types. Thank you for providing the code!

Although the original issue is solved by avoiding distribution, I'm still not sure why Function comes in the result type as the inferred type parameter T is not relevant type of Function and Function part of PropType does not include type parameter T. Would you mind telling me how type inference works under the hood in this case?

@typescript-bot
Copy link
Collaborator

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants