microsoft / microsoft/TypeScript

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

Aperta
#63,425 6 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Experimentation Needed Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con la riproduzione in Playground e confronta le visualizzazioni al passaggio del mouse dei mapped types direct, homomorphic e non-homomorphic descritte nell’issue. Leggi il percorso di normalizzazione discusso nell’issue #47214 e verifica che le chiavi stringa dall’aspetto numerico rimangano racchiuse tra virgolette; il lavoro è completato quando B viene visualizzato come { '1': "1"; } senza modificare la visualizzazione delle chiavi numeriche.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Tranquilla
Chiarezza
Specificata chiaramente
Idoneità per principianti
65/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.