microsoft / microsoft/TypeScript

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

Ouverte
#63,425 6 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Experimentation Needed Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
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
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.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par la reproduction dans Playground et comparez les affichages au survol des mapped types direct, homomorphic et non-homomorphic décrits dans l’issue. Lisez le chemin de normalisation abordé dans l’issue #47214 et vérifiez que les clés de chaîne ressemblant à des nombres restent entre guillemets ; c’est terminé lorsque B est rendu comme { '1': "1"; } sans modifier l’affichage des clés numériques.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Bug
Difficulté
3/5
Temps estimé
1-2 jours
Activité
Calme
Clarté
Clairement spécifiée
Accessibilité débutants
65/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.