microsoft / microsoft/TypeScript

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

Open
#63,425 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Experimentation Needed Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Playground reproduction and compare the direct, homomorphic, and non-homomorphic mapped-type hover displays described in the issue. Read the normalization path discussed in issue #47214 and verify that numeric-looking string keys remain quoted; done means B renders as { '1': "1"; } without changing the numeric-key display.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
65/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.