microsoft / microsoft/TypeScript
Support declaring multiple setter overloads
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
🔍 Search Terms
setter overload, multiple setter signatures, multiple setter input types
✅ Viability Checklist
- 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, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ 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) {
// ...
}
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Überprüfe die vorgeschlagene Syntax für Setter-Überladungen und das begründende Beispiel im Issue. Ermittle, wie mehrere Setter-Signaturen typgeprüft werden sollten, während eine Implementierung und eine unveränderte JavaScript-Ausgabe erhalten bleiben; abgeschlossen ist die Aufgabe, wenn das Feature unterstützt wird, ohne das bestehende Laufzeitverhalten zu ändern.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 35/100