microsoft / microsoft/TypeScript

Use a consistent ordering when writing union types

Offen
#17,944 5 Kommentare 31 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Domain: LS: Type Display Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

In DefinitelyTyped, $ExpectType assertions depend on a type having a consistent string represenation across TypeScript versions.
It looks like the ordering of union types is dependent on some implementation details that change between versions. When we output unions as strings, we should sort them in some consistent way first instead of just using whatever order we happened to use internally.

I would suggest this ordering:

  • numeric literals, low to high
  • string literals, low to high (by <)
  • named types (including type aliases, enum, class and interface names), by name
  • function literals: by length (() => void before (x: number) => void), then by parameter name, then by parameter type
  • type literals: Sorted smallest to largest ({ x: number } before { x: number, y: number }); and type literals of the same size should be alphabetically sorted by property ({ a: number }) before { b: number }) or by the sorting of values ({ s: "a" } before { s: "b" })

Of course, another solution would be to try to handle this in $ExpectType by parsing out unions and allowing it if any sorting is valid. But it is strange in TS to see a string literal union displayed in a seemingly random order.

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

Es wird keine Datei und kein Test genannt. Beginne damit, den Compilerpfad nachzuverfolgen, der Union-Typen in Zeichenfolgen umwandelt, und vergleiche ihn dann mit den DefinitelyTyped-Assertions $ExpectType; als erledigt gilt die Aufgabe, wenn die Union-Mitglieder über verschiedene TypeScript-Versionen hinweg einer deterministischen Reihenfolge folgen.

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
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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