Skip to content

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

Open
@Andarist

Description

@Andarist

🔎 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Possible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some cases

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions