microsoft / microsoft/TypeScript
Use of intersection type leads to extremely slow typecheck times
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
🔎 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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia dal file collegato convex-ents src/functions.ts e riproduci la EntWriter-to-Ent assignment che attiva il rallentamento. Confronta il controllo dei tipi con i rami condizionali originali di PromiseEdgeWriter e la versione ottimizzata di PromiseEdgeResult, usando tracce del compilatore o misurazioni dei tempi per isolare il metodo edge e il controllo delle intersezioni. Il lavoro è completo quando viene identificato un collo di bottiglia riproducibile nel typechecker di TypeScript e viene stabilito un miglioramento validato o una limitazione documentata.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers, performance
- Tipo di issue
- Bug
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 25/100