microsoft / microsoft/TypeScript

Type alias preserving logic defeats complexity reduction mechanisms

Offen
#54,146 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bug Domain: check: Big Unions
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

Playground link with relevant code

💻 Code
// Just a short block to make a union with > 300 unique members
export type Bit = 0 | 1;
export type SyntaxKind = `${Bit}${Bit}${Bit}${Bit}${Bit}${Bit}${Bit}${Bit}${Bit}`
type NodeMaker<T extends SyntaxKind = SyntaxKind> = T extends T ? {kind: T}: never;

type Node = NodeMaker;
type Ok = Node & Node;

type Node2 = NodeMaker;
type Wat = Node & Node2; // fails to recognize Node and Node2 are identical, issues bogus complexity error
🙁 Actual behavior

On the Wat declaration, we issue a complexity error, even though Node and Node2 are identical.

🙂 Expected behavior

No complexity error, like on the Ok line.

The Wat case is actually easy to trigger in real code, as it's what happens in control flow when a type is removed and then readded to the control flow narrowed union - you get the same type, but with no alias information. I bring this up because we trivially trigger this same issue in a handful of places in our own union-ized code base.

Workaround

In type-space, none, sadly (short of eliminating the duplicated types). In expression-space, casting one of the expressions that's lost its' alias back to the aliased form can remove the error, though this feels bad, since the cast isn't well checked.

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

Führe zunächst die verknüpfte TypeScript Playground-Reproduktion aus und vergleiche die Deklarationen von Ok und Wat, wobei du dich auf das Verhalten von Typalias und Komplexitätsreduzierung konzentrierst. Verfolge den Compiler-Einstiegspunkt, der für die Prüfung dieser Schnittmengen verantwortlich ist, und füge einen Regressionstest hinzu, der zeigt, dass identische Node- und Node2-Typen keinen Komplexitätsfehler erzeugen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
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.