microsoft / microsoft/TypeScript
Generics in union should be inferred as `never` when possible
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
### 🔎 Search Terms
generics, inference, union, never
### 🕗 Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about union and never.
### ⏯ Playground Link
https://www.typescriptlang.org/play/?#code/MYewdgzgLgBAZjAvDAPAFQDQD4AUAPALhjRgB8ZoAnASzAHMMYBPIk8sAVwFsAjAU0oBKJFmYAoODgDkAQymMAjMIgyo1CHGp8IMTrwEwA9IeIx1MAEQyLukAHcxY0JFh0kqEnzxQ+YACY6HP58mmB8friEpuzc-EIiMHhidDhKFKrqmtq6sQbGpuZBfiG04bZ2QA
### 💻 Code
```ts
const f = (x: T | string, y: T | number) => y
f('a', 1) satisfies number // T is "a" now
const g = (x: T | number) => x
g(1) satisfies number // T is undefined now
```
### 🙁 Actual behavior
`T` is not `never`.
### 🙂 Expected behavior
`T` should be inferred as `never`, because it can be anything.
### Additional information about the issue
This may be related to #42491.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked TypeScript Playground reproduction and compare its generic inference behavior with the related issue #42491. The work is complete when T is inferred as never in both examples and the satisfies checks produce the expected results.
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
- Mostly clear
- Newbie friendliness
- 35/100