microsoft / microsoft/TypeScript

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

未关闭
#63,425 6 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Experimentation Needed Suggestion
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

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

```ts
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:

```typescript
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.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 Playground 复现开始,比较 issue 中描述的 direct、homomorphic 和 non-homomorphic mapped type 悬停显示。阅读 issue #47214 中讨论的规范化路径,并验证看起来像数字的字符串键仍然带引号;当 B 渲染为 { '1': "1"; } 且不改变数字键的显示时,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
typescript
领域
compilers
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
冷清
描述清晰度
描述清楚
新手友好度
65/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。