microsoft / microsoft/TypeScript

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

Đang mở
#63,132 2 bình luận 0 reaction 1 người được giao Xem trên GitHub

@ahejlsberg đang làm issue này rồi.

Từ ngày 13/2/2026.

Needs Investigation
Ngôn ngữ chính
Go
Star
111k
Fork
14.3k
Merge trung bình
2 ngày 4 giờ
Pull request đã merge (30 ngày)
132

Mô tả

🔎 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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.