microsoft / microsoft/TypeScript

Reverse mapped type fails to be inferred with constraint that is a union containing an intersection

Open
#56,910 0 comments 0 reactions 0 assignees View on GitHub

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
⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.4.0-dev.20231231#code/CYUwxgNghgTiAEAzArgOzAFwJYHtVNQB4AVeEADwxFWAGd4AlcHGYQ2jGLVAcwBp4qZAFsARiBgA+SQAocogFYAueAG8AUPHgBtANLxu8GQGsQATxyJ4pAGTwARFHsBKeAB8Ho+wF0VxPd4A3OoAvs5+wWB4HPDyCvAAvGqa8FAqACwATHwpoioAjAAMhTlaYCqcyCA5IZHRGPBwtMgQDUmIqHKKzsEA9L1aWgB6APzqQA

💻 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

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

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.