microsoft / microsoft/TypeScript

Regression: Mapped types are no longer homomorphic when wrapped in certain conditional types

Offen
#63,132 2 Kommentare 0 Reaktionen 1 zugewiesene Person Auf GitHub ansehen

@ahejlsberg arbeitet bereits daran.

Seit 13.2.2026.

Needs Investigation
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

🔎 Search Terms

"homomorphic" "keyof" "extends"

🕗 Version & Regression Information
  • This changed between versions 3.8.3 and 3.9.7
⏯ Playground Link

https://www.typescriptlang.org/play/?#code/C4TwDgpgBA0hIFkCGYA8AVAfFAvFA1vAPYBmU6UEAHsBAHYAmAzlAAoBORk7ocIUAfigBvKAG0YUAJZ0CxMugC6ALlhQAvlFV0IANwjsA3AChjoSFAAauWPGRo6AVwC2AIwOZDQA

💻 Code
type KeyMap<T> = keyof T extends PropertyKey ? { [K in keyof T]: K } : never;

type X = KeyMap<number>;
🙁 Actual behavior

X is evaluated to

type X = {
    toString: "toString";
    toFixed: "toFixed";
    toExponential: "toExponential";
    toPrecision: "toPrecision";
    valueOf: "valueOf";
    toLocaleString: "toLocaleString";
}
🙂 Expected behavior

X should be evaluated to number because according to the FAQ,

Mapped types declared as { [ K in keyof T ]: U } where T is a type parameter are known as homomorphic mapped types, which means that the mapped type is a structure preserving function of T. When type parameter T is instantiated with a primitive type the mapped type evaluates to the same primitive.

Additional information about the issue

Replacing PropertyKey with either unknown or any fixes the issue for some reason.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.