microsoft / microsoft/TypeScript
Mapped type of a conditional type unexpectedly fails inference
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 Search Terms
conditional type infer unknown
conditional type mapped type inference
🕗 Version & Regression Information
This is the behavior in every version I tried (3.3.3 to 5.5.3), and I reviewed the FAQ for entries about conditional types, mapped types, and structural inference.
⏯ Playground Link
💻 Code
type Mapped<T> = { [P in keyof T]: T[P] }
type ComponentA = Mapped<{ field: number }>
type ComponentB = { field: number }
type Infer1<T> = Mapped<T extends never ? null : T>
declare function infer1<T>(c: Infer1<T>): T
infer1({} as ComponentA).field = 1 // ✅
infer1({} as ComponentB).field = 1 // ❌ Error: Object is of type 'unknown'
type Infer2<T> = T extends never ? null : Mapped<T>
declare function infer2<T>(c: Infer2<T>): T
infer2({} as ComponentA).field = 1 // ✅
infer2({} as ComponentB).field = 1 // ✅
🙁 Actual behavior
infer1(ComponentA) passed inference but infer1(ComponentB) failed with unknown.
However, if it's the other way around — Conditional ? Mapped<T> instead of Mapped<Conditional ? T>, both infer2(ComponentA) and infer2(ComponentB) passed inference.
🙂 Expected behavior
I expect infer1(ComponentA) and infer1(ComponentB) to both work, because ComponentA and ComponentB are essentially the same type — I'm unable to otherwise differentiate them by any means.
Additional information about the issue
I discovered this while debugging vuejs/core#11353, and the interference failure there was eventually reduced to this report.
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 and the reduced infer1/infer2 example, then compare how the two conditional and mapped-type arrangements infer T for ComponentA and ComponentB. Done means infer1 accepts both equivalent component shapes without producing unknown, with the behavior covered by a suitable regression test.
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