microsoft / microsoft/TypeScript

[Feature Request] Preserve comments when using Extract<keyof T, string>

Ouverte
#31,992 5 commentaires 1 réaction 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Experience Enhancement Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.4k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

Description

Search Terms

mapped type, preserve comment, keyof, Extract

Suggestion

type Mapped<T> = {
  [k in keyof T]: ["some transformation", T[k]]
};

interface IFoo {
  /** The string */
  x: string;
  /** The number */
  y: number;
}
declare const mappedIFoo: Mapped<IFoo>;
//Tooltip shows "The string" as the comment
mappedIFoo.x

//////////////////////////
type Mapped2<T> = {
  [k in Extract<keyof T, string>]: ["some transformation", T[k]]
};

declare const mapped2IFoo: Mapped2<IFoo>;
//Tooltip DOES NOT show "The string" as the comment
mapped2IFoo.x

Playground

I'd like it if the fields of the mapped type could somehow preserve the comments of the fields of T, even after using Extract<keyof T, string>.

Use Cases

In my projects, there are many cases where I only want to deal with string keys and not symbol|number keys. So, I use Extract<keyof T, string> a lot. However, this does not preserve comments and makes me sad =(

Examples

//--noImplicitAny
type Mapped<T extends { [k: string]: any }> = {
  [k in keyof T]: ["some transformation", T[k]]
};
const someSymbol: unique symbol = Symbol();

interface IFoo {
  /** The string */
  x: string;
  /** The number */
  y: number;
  1: "i am a number";
  [someSymbol] : "i am a symbol"
}
declare const mappedIFoo: Mapped<IFoo>;
//Tooltip shows "The string" as the comment
mappedIFoo.x;
//Is allowed, because we use `keyof T`
mappedIFoo[1];
//Is allowed, because we use `keyof T`
mappedIFoo[someSymbol];

//////////////////////////
type Mapped2<T extends { [k : string] : any }> = {
  [k in Extract<keyof T, string>]: ["some transformation", T[k]]
};

declare const mapped2IFoo: Mapped2<IFoo>;
//Expected: Tooltip shows "The string" as the comment
//Actual:   Tooltip DOES NOT show "The string" as the comment
mapped2IFoo.x;
//Expected: is not allowed
//Actual  : Is not allowed; OK!
mapped2IFoo[1];
//Expected: is not allowed
//Actual  : Is not allowed; OK!
mapped2IFoo[someSymbol];

Playground

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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 les exemples TypeScript et les Playgrounds fournis, en comparant le comportement des tooltips de Mapped avec celui de Mapped2 à l’aide de Extract<keyof T, string>. Le travail est considéré comme terminé lorsque des commentaires tels que « The string » et « The number » sont conservés pour les champs mappés, tandis que les clés number et symbol restent exclues ; aucun fichier ni test du dépôt n’est nommé.

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

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Fonctionnalité
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

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