microsoft / microsoft/TypeScript

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

Aperta
#31,992 5 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

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.

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 gli esempi TypeScript e i Playgrounds forniti, confrontando il comportamento dei tooltip di Mapped con quello di Mapped2 usando Extract<keyof T, string>. Il lavoro è completo quando commenti come “The string” e “The number” vengono mantenuti per i campi mappati, mentre le chiavi number e symbol rimangono escluse; non vengono nominati file o test del repository.

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

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.