question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. Itย collects links to all the places you might be looking at while hunting down a tough bug.

And, if youโ€™re still stuck at the end, weโ€™re happy to hop on a call to see how we can help out.

Inferred type is not the same as what's passed in original generic type.

See original GitHub issue

Bug 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

https://www.typescriptlang.org/play?ts=4.1.3#code/C4TwDgpgBACgTgezAYQQOwM7DgVwMbAJwA8AKlALxQCGaIAfJQFBRQA+UA3lGhAO5QAFADpR1OAHMMALhp0A2gF0AlLPIAyKAgBGAKwgEoAXxbsuQ1VHImmoSFACyIAJJoAZhBKlGVchAAewBBoACYYsIgo6Fi4BETEAJbunlAAaowA-GlQsgDk-gW5TLbg0KnllEKCypSMMUkSNRyC1bU8OAC22p7KxXbQABqVTq4eJPBIqJjY+IQk5en0TEA

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:open
  • Created 3 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

6reactions
RyanCavanaughcommented, Jan 27, 2021

โ€œ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

2reactions
dragomirtitiancommented, Jan 27, 2021

Interesting to note that these two definitions

type PropConstructor<T = any> =
  { new (...args: any[]): T & object }

Playground Link

and

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

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found