microsoft / microsoft/TypeScript

Use of intersection type leads to extremely slow typecheck times

Offen
#58,559 9 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Needs More Info
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔎 Search Terms

"intersection", "performance", "typecheck", "subtype", "time"

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about performance
⏯ Playground Link

No response

💻 Code
// Your code here
🙁 Actual behavior

I have a pretty complicated library: https://github.com/xixixao/convex-ents (it's similar to Prisma)

(I'm gonna describe the issue in-situ, because essentially the whole library is involved to get the times I'm observing)

All the code I'll mention is in this file: https://github.com/xixixao/convex-ents/blob/edgebitfaster-more-deduped/src/functions.ts

In it I have two intersection types, Ent and EntWriter. I have to use an intersection, because I want to add methods to a POJO (I do this via an intersection between a class and a Record type).

One type should be a subtype of the other (EntWriter should be a subtype of Ent).

The types have a method edge (from the class in the intersection), which returns a conditional type (PromiseEdge and PromiseEdgeWriter respectively), which results in other complicated types, but the subtyping relationship holds.

The library code typechecks fine, but the issue arises when the user of the library tries to assert that EntWriter<> is indeed a subtype of Ent:

const x: Ent<"sometable"> = someEntWriter;

Now TypeScript goes and tries to check the subtyping relationship, starting with the intersection, walking to the conditionals, and to all their branches, and to all the methods of those types, etc. It took minutes.

I can observe the slowdown by reverting PromiseEdgeWriter to match PromiseEdge exactly, and then changing one branch at a time.

After reading the performance wiki and getting a trace and going through the types, I have optimized this somewhat. I have pulled out the conditional logic into a type that is reused (see PromiseEdgeResult). This helped a lot, but the typechecking still takes 12 seconds instead of sub-second without this edge type change.

I tried pulling out the intersection to a helper type as well (EntBase) but this had no impact. It seems that the intersection is "inlined"?

🙂 Expected behavior

What I think would help me is if:

A subtype of B
X' = X<A, C>
X'' = X<B, C>
implied that X' subtype of X''
even when X<T, U> = T & U

But this doesn't seem to be what the typechecker actually assumes? (probably because intersection merged properties as per wiki)

But could I hint somehow to the compiler to not go through this whole huge reasoning chain? I know that EntWriter is subtype of Ent when their generic arguments are the same.

The intersection types themselves are ok, but it's the interplay with the two classes. The slowdown is from the edge method being checked. So alternatively I'd like to tell TypeScript: "Hey, this method does correctly override the parent class's method, you don't need to check that if the generic arguments to it are the same"

Additional information about the issue

No response

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

Beginnen Sie mit der verknüpften Datei convex-ents src/functions.ts und reproduzieren Sie die EntWriter-to-Ent assignment, die die Verlangsamung auslöst. Vergleichen Sie die Typprüfung mit den ursprünglichen bedingten Zweigen von PromiseEdgeWriter und der optimierten Version von PromiseEdgeResult und verwenden Sie Compiler-Traces oder Zeitmessungen, um die Edge-Methode und die Prüfung von Schnittmengen zu isolieren. Als abgeschlossen gilt die Aufgabe, wenn ein reproduzierbarer TypeScript-Typprüfer-Engpass identifiziert und eine validierte Verbesserung oder dokumentierte Einschränkung festgestellt wurde.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

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