microsoft / microsoft/TypeScript

Support declaring multiple setter overloads

Aperta
#60,664 2 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔍 Search Terms

setter overload, multiple setter signatures, multiple setter input types

✅ Viability Checklist
⭐ Suggestion

TypeScript allows declaring multiple different argument signatures for a function, i.e. "function overloads": https://www.typescriptlang.org/docs/handbook/2/functions.html#function-overloads.

We should be able to do the same for property setters, since they are modeled much like a function taking a single argument.

(Note that unlike some other languages, TypeScript's overloads are only bare type signatures: there is only a single method body implementation shared by all of them, making this a pure typechecking feature with no impact on the generated runtime code. This proposal works the same way, just extending this syntax from functions/methods to setters as well).

📃 Motivating Example

A setter might want to separate different types of inputs for improved clarity of documentation:

/**
 * Set startTime to a specific timestamp, specified as a Date object or number of ms since epoch.
 */
set startTime(date: Date | number);

/**
 * Set startTime to the start of the most recent activity matching the given category name.
 */
set startTime(category: string);

set startTime(dateOrCategory: Date | number | string) {
    // ...
}

Note that these differ not just in documentation but also in the value argument's name, which often appears in generated docs output too.

💻 Use Cases

Although setter overloads are necessarily less versatile than function overloads with multiple parameters, some of the same rationales for the overload feature still apply to setters – as seen in the example above.

Workaround
As with functions before overloading is supported, the workaround is just to glom all the docs together with some additional verbiage, e.g.:

/**
 * Set startTime:
 * - If given a Date object or number of ms, sets to a specific timestamp.
 * - If given a category name string, sets to the start of the most recent activity matching that name.
 */
set startTime(dateOrCategory: Date | number | string) {
    // ...
}

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

Esamina la sintassi proposta per gli overload dei setter e l’esempio motivante nell’issue. Determina come debbano essere verificati i tipi di più firme di setter mantenendo una sola implementazione e un output JavaScript invariato; il lavoro è completo quando la feature è supportata senza modificare il comportamento esistente a runtime.

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
Specificata chiaramente
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.