microsoft / microsoft/TypeScript

Use of intersection type leads to extremely slow typecheck times

Abierto
#58,559 9 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Needs More Info
Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

🔎 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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza con el archivo vinculado convex-ents src/functions.ts y reproduce la EntWriter-to-Ent assignment que provoca la ralentización. Compara la comprobación de tipos con las ramas condicionales originales de PromiseEdgeWriter y la versión optimizada de PromiseEdgeResult, usando trazas del compilador o mediciones de tiempo para aislar el método edge y la comprobación de intersecciones. Se considera terminado cuando se haya identificado un cuello de botella reproducible del comprobador de tipos de TypeScript y se haya establecido una mejora validada o una limitación documentada.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
typescript
Área
compilers, performance
Tipo de issue
Error
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Necesita aclaración
Aptitud para principiantes
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.