Inferred type is not the same as what's passed in original generic type.
See original GitHub issueBug Report
π Search Terms
infer, generic
π Version & Regression Information
- This is a crash
- This changed between versions _________ and _________
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about infer.
- I was unable to test this on prior versions because _______
β― Playground Link
Playground link with relevant code
π» Code
// The variable names are meaningless, they are simplified from vue project.
type PropConstructor<T = any> =
| { new (...args: any[]): T & object }
| { (): T }
type MyInfer<T> = T extends PropConstructor<infer V> ? V : 'xxx'
type VVV = (() => string) | (() => number)
type X = MyInfer<PropConstructor<VVV>>
π Actual behavior
type X = (() => string) | (() => number) | ((() => string) & object) | ((() => number) & object)
π Expected behavior
type X = (() => string) | (() => number))
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
Returned type of a function is not inferred when it's ...
I thought maybe the problem is that name property of SchemaItem is declared as string and here is what I come up with....
Read more >Generic Methods - Learning Java, 4th Edition [Book]
We've seen a generic method infer its parameter type from its argument types. But what if the type variable isn't used in any...
Read more >Documentation - Generics
Once we've written the generic identity function, we can call it in one of two ways. The first way is to pass all...
Read more >Inferring types in a conditional type
When item2 is constructed, the condition in the conditional type is false because {name: string} does not match (infer E)[] . Therefore the...
Read more >What Are Generics in TypeScript?
Line 6: This is totally valid error, as data has a generic type and it can be either String, Number, Float, or any...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
βWhatβs the harm in adding leading
|
as syntactic sugarβ, they said! πThis should be a straightforward fix - union/intersection redistribution should not occur on single-constituent unions
Interesting to note that these two definitions
Playground Link
and
Playground Link
Behave differently when used in the conditional type the first one will keep
(() => string) | (() => number)
as expected, while the second will infer((() => string) & object) | ((() => number) & object)
.I would not have expected a union of one constituent to behave differently to that constituent.