microsoft / microsoft/TypeScript

Feature: Composing spread operator on Union types and static_key function to enable generic capability

Offen
#17,061 9 Kommentare 38 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Needs Proposal Suggestion
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

  1. Add spread operator ... to string literal union types acting as an string array or generator. Type T applying spread operator will result in type T[]. For example:
type Group = 'red' | 'blue' | 'green';
const groups = ...Group; // groups has type of Group[] with values ['red', 'green', 'blue']
  1. Add static function static_key or keyof that 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'
  1. 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 Record type 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

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Ö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

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.