Conditional types infer
gets wrong type if it inferred from union type
#37734
Labels
Question
An issue which isn't directly actionable in code
TypeScript Version: 3.9.0-dev.20200330
Search Terms: infer function union conditional types
Code
Expected behavior:
Res
is of typeA
Actual behavior:
Res
is of typeFunction | 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 inPropType
. SincePropType
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 forFunction
constructor)To use the props type, we need to extract actual type from
PropType
generics (ExtractProp
in reproduction). We want to extract typeA
if the constructor type is ofPropType<A>
. But the actual behavior isFunction | A
.I also found if I pass
PropType
via another object type, it infers correctly.https://www.typescriptlang.org/play/#code/JYOwLgpgTgZghgYwgAgILIN4FgBQzmQDOYAXMiBAG7S4C+uuYAngA4oAKUA9iwCqsQAPLwB8yALzJc+AD6ZyEAO4AKAHTq4UAOaEycEEwDaAXQCUZXsgBkyLgCMAVhARhk9PMjkYFK9as06ZMRQoFom5sgAYgCuIC7AXCBuDDjMbMgAogAeYFCIYJw8guxikuzIEDkQIAAmhPJpEGSFfAKCoDDQyKJuyAD83chkFNRQKY3IAEoQ9ZLZufktgt6NzdytbIKoYrRiAPR7aLhAA
The text was updated successfully, but these errors were encountered: