microsoft / microsoft/TypeScript

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

Open
#42,516 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: check: Type Inference Effort: Difficult Help Wanted
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the linked TypeScript Playground examples and comparing the inferred type X with the reported actual and expected types. The work is complete when the generic inference behavior for MyInfer<PropConstructor> matches the expected union without the additional intersections.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.