microsoft / microsoft/TypeScript

an emitter with 239 events breaks type check, throw ts2590 Expression produces a union type that is too complex to represent

Offen
#42,790 3 Kommentare 4 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Domain: check: Big Unions Domain: check: Contextual Types Needs Investigation
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

Bug Report

I wrote a framework (namely koishi) which uses a fully-typed event emitter implemented by itself. However when I create more events (totally 239), the type check just broke with an error ts2590 (Expression produces a union type that is too complex to represent). It seems that the error was thrown here:

image

I know 239 events is a lot, but I still cannot figure out how the 239 events became "100000 type checks".

Can you help prevent this error either by optimize type check performance or by improve my code? Thanks.

🔎 Search Terms

union type, interface, event emitter, ts2590

🕗 Version & Regression Information

I tried with v4.1.3, v4.1.5 & v4.2.1-rc. They all failed.

⏯ Playground Link

This playground will not work because it imports a dependency @octokit/webhooks-definitions (it's just a simple collection of github webhooks type definitions, but it is too big (over 5000 lines) to be included as I thought).

Playground link with a dependency

And the dependency code can be found here: https://unpkg.com/@octokit/webhooks-definitions@3.60.0/schema.d.ts

💻 Code
interface EventMap extends SessionEventMap, WebhookEventMap {}

declare function on<K extends keyof EventMap>(name: K, listener: EventMap[K]): () => boolean

on('message', () => {})
🙁 Actual behavior

TS2590 error: Expression produces a union type that is too complex to represent.

🙂 Expected behavior

No compiler error

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

Beginne mit dem verlinkten Playground und dem minimalen EventMap/on-Beispiel. Untersuche anschließend die referenzierte Abhängigkeit @octokit/webhooks-definitions schema.d.ts, um TS2590 mit ungefähr 239 Events zu reproduzieren. Vergleiche die gemeldeten TypeScript-Versionen und bestätige, dass das Beispiel ohne den Fehler aufgrund der Komplexität der Union-Typen typgeprüft wird.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
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.