microsoft / microsoft/TypeScript

Hover display for non-homomorphic mapped types renders numeric-string keys indistinguishably from numeric keys

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

Chưa có ai nhận issue này.

Experimentation Needed Suggestion
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

mapped type hover, numeric string key display, key quoting, homomorphic mapped type

🕗 Version & Regression Information

This is the behavior in every version I tried.

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAglC8UDeBtA0lAlgOygRgF0AuKNAXwG4AoAehqgYD0B+K0SKAIQWXS1wDkeAcVKVa9JsyA

💻 Code
type A = {[K in 1]: K};
//   ^? type A = { 1: 1; }
type B = {[K in '1']: K};
//   ^? type B = { 1: "1"; }
🙁 Actual behavior

A and B have keys of different types (1 vs "1"), but the hover renders both keys identically as 1. In B, the display shows key 1 mapping to value "1" — the key and value originate from the same K, so they must be the same type, yet the display suggests they're not.

🙂 Expected behavior

The printer generally prefers the simpler key representation when it's unambiguous (abc rather than "abc"), but numeric literals are themselves valid keys, so numeric-looking strings need to stay quoted to avoid collapsing into a different type. This safeguard is applied correctly in the direct case:

type Direct = {'1': 'a'};
//   ^? type Direct = { '1': "a"; }

but is lost on the non-homomorphic mapped-type path. B's expected display is { '1': "1"; }.

Additional information about the issue

A related issue (#47214) discussed this for a simpler repro and was closed with the rationale that mapped-type results go through a normalization path without an original spelling to echo. That rationale fits cases where quoting is purely cosmetic; here the unquoted form renders B in a way that obscures which type its keys actually are, so the quoting isn't a style preference but part of what distinguishes string-keyed from number-keyed types in the display.

Note: {[K in keyof T]: ...} renders correctly because it matches the homomorphic mapped type pattern, which preserves the original key representation. Constraints that don't match this pattern, force the general normalization path where the quoting is lost.

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.

Hướng nghiên cứu

Bắt đầu với bản tái hiện trong Playground và so sánh các hiển thị khi di chuột của các mapped type direct, homomorphic và non-homomorphic được mô tả trong issue. Đọc đường dẫn chuẩn hóa được thảo luận trong issue #47214 và xác minh rằng các khóa chuỗi trông giống số vẫn được đặt trong dấu ngoặc kép; được xem là hoàn tất khi B được hiển thị dưới dạng { '1': "1"; } mà không thay đổi cách hiển thị khóa số.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
typescript
Lĩnh vực
compilers
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
65/100

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.