microsoft / microsoft/TypeScript

Show comments from properties referenced via 'keyof T'

Aperta
#41,220 1 commento 4 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

In Discussion Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Search Terms

comments, keyof

Suggestion

The goal would be to improve Quick Info using code comments extracted from members via a generic keyof T when used as part of a call expression.

Consider the following two cases:

// old way of writing events (since forever)
interface Evented1 {
  /** Handles the 'click' event. */
  on(type: "click", listener: (args: ClickEvent) => void): this;
  /** Handles the 'mousedown' event. */
  on(type: "mousedown", listener: (args: MouseDownEvent) => void): this;
  // ...
}

declare const obj1: Evented1;
obj1.on("click", _ => {}); // shows 'Handles the 'click' event in quickinfo when hovering over 'on'.
obj1.on("mousedown", _ => {}); // shows 'Handles the 'mousedown' event in quickinfo when hovering over 'on'.


// new way of events (used in lib.dom, may end up being used in Node at some point)...
interface Evented2EventMap {
  /** Handles the 'click' event. */
  click: ClickEvent;
  /** Handles the 'mousedown' event. */
  mousedown: MouseDownEvent;
  // ...
}

interface Evented2 {
  on<K extends keyof Evented2EventMap>(type: K, listener: (args: Evented2EventMap[K]) => void): this;
}

declare const obj2: Evented2;
obj2.on("click", _ => {}); // no comments when hovering over 'on'.
obj2.on("mousedown", _ => {}); // no comments when hovering over 'on'.

Use Cases

Improving quick-info for event-based methods like on/addListener/addEventListener (i.e. for EventEmitter, EventTarget, etc.) and messaging based methods like send (on message ports, websockets, etc.).

Additionally, we could leverage our new @deprecated JSDoc-tag support to flag calls when a generic argument refers to a member name that has been marked with @deprecated.

Examples

interface RequestMap {
  /** @deprecated Misspelled, use `AddItem` instead. */
  AddIetm: [AddItemRequest, AddItemResponse];

  /** Adds a new item to the collection */
  AddItem: [AddItemRequest, AddItemResponse];
}

interface EventMap {
  /** Raised whenever an item is added to the collection. **/
  CollectionChanged: CollectionChangedEventArgs;
}

interface Service {
  /** Sends a request to the service. */
  send<K extends keyof RequestMap>(type: K, args: RequestMap[K][0]): Promise<RequestMap[K][1]>;

  /** Listens for an event from the service. */
  on<K extends keyof EventMap>(type: K, listener: (args: EventMap[K]) => void): this;
}

declare const svc: Service;

Quick Info for send:

service.send("AddItem", { }); 
(method) Service.send<"AddItem">(type: "AddItem", args: AddItemRequest): Promise<AddItemResponse>

Sends a request to the service.

Adds a new item to the collection.

Quick Info for send with deprecation:

service.send("AddIetm", {});
(method) Service.send<"AddIetm">(type: "AddIetm", args: AddItemRequest): Promise<AddItemResponse>

Sends a request to the service.

@deprecated - Misspelled, use AddItem instead.

Quick Info for on:

service.on("CollectionChanged", args => {});
(method) Service.on<"CollectionChanged">(type: "CollectionChanged", listener: (args: CollectionChangedEventArgs) => void): Service

Listens for an event from the service.

Raised whenever an item is added to the collection.

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.

Related Issues

  • #31992 - Preserve comments when using Extract<keyof T, string>
  • #41165 - 'Documented' Utility Type

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 di Quick Info nell’issue e consulta le issue correlate #31992 e #41165. Il lavoro è completato quando i commenti e le informazioni sulla deprecazione dei membri referenziati tramite keyof T vengono visualizzati in Quick Info per le chiamate send e on mostrate, senza modificare il comportamento a runtime.

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

Valutazione

Stack tecnologico
typescript
Ambito
compilers, developer-experience
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.