microsoft / microsoft/TypeScript
Reverse mapped type fails to be inferred with constraint that is a union containing an intersection
Open
Nobody has claimed this yet.
Domain: check: Type Inference
Possible Improvement
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 Search Terms
reverse mapped type inference inferred constraint union intersection
🕗 Version & Regression Information
- This is the behavior in every version I tried. However, I'd call this to only be a bug for recent 5.4 nightlies - this should only work since https://github.com/microsoft/TypeScript/pull/55811
⏯ Playground Link
💻 Code
declare function fn<T extends Record<string, number>>(obj: {
[K in (keyof T & "a") | "b"]: T[K];
}): T;
const obj = {
a: 42,
b: 100,
c: true,
};
const result = fn(obj);
🙁 Actual behavior
It fails to infer T and uses its constraint
🙂 Expected behavior
It should infer { a: number; b: number; }
Additional information about the issue
No response
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
Reproduce the behavior using the linked TypeScript Playground and the provided fn/object example. Trace why inference for T falls back to its constraint; the work is done when result is inferred as { a: number; b: number; } rather than the constraint, with a regression test covering the example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100