microsoft / microsoft/TypeScript
Feature: Composing spread operator on Union types and static_key function to enable generic capability
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
Related issues: #13542 #15759 (slightly different take and example on closed issues)
I originally posted on issue #15759 but decided that they were different enough to be looked at separately and that issue doesn't address a generic capability. Look back at https://github.com/Microsoft/TypeScript/issues/15759#issuecomment-314157616 for clunky workaround examples.
Proposal
- Add spread operator
...to string literal union types acting as an string array or generator. TypeTapplying spread operator will result in typeT[]. For example:
type Group = 'red' | 'blue' | 'green';
const groups = ...Group; // groups has type of Group[] with values ['red', 'green', 'blue']
- Add static function
static_keyorkeyofthat transforms a static interface to a string literal union of all combined types. For example:
interface Groups<T, U, V> {
red: T;
green: U;
blue: V;
}
type groups = static_key(Groups); // 'red' | 'blue' | 'green'
- Composing both can result in powerful generic operations. I feel that this could bring a unique capability that string enums could not and may have been overlooked. It would be very powerful for a
Recordtype or interface where all fields are the same type. Here is an example:
interface GroupDescription {
name: string;
description: string;
}
type Group = 'red' | 'blue' | 'green';
type Groups = { [K in Group]: GroupDescription };
function initGroups(groups: Groups) {
for(const group of ...static_key(Groups)) { // group would be type Group or 'red' | 'blue' | 'green'
processGroup(name); //Generically Process property
}
}
function processGroup(group: Group) { }
I noticed I don't need ...static_key but instead directly use the spread operator on the union type ...Group. The point is that they compose well together and they add generic capability.
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
Das Issue nennt keine Dateien, Tests oder Einstiegspunkte. Beginne damit, den Vorschlag und die verwandten Issues #13542 und #15759 einschließlich des verlinkten Workaround-Kommentars zu lesen, und lege dann ein abgestimmtes Design für Union-Spreading und static_key fest. Als erledigt gilt die Aufgabe, wenn der Umfang und die Implementierungskriterien von einem Maintainer genehmigt wurden.
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
- Muss geklärt werden
- Anfängerfreundlichkeit
- 25/100