microsoft / microsoft/TypeScript
type checking complexity with multiple template literals in unions
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 2 d 4 h
- PR fusionados (30 d)
- 132
Descripción
### 🔎 Search Terms
template literals, performance
### 🕗 Version & Regression Information
4.1 introduced template literals where `tsc` fails with `RangeError: Map maximum size exceeded`.
4.5 is the first version that completes where typechecking time already scales exponentially.
### ⏯ Playground Link
https://github.com/eps1lon/repro-ts-template-literals-complexity
### 💻 Code
The example is based on how Next.js typechecks the `href` in `` components.
```ts
// Example type definitions for Next.js routes
type SearchOrHash = `?${string}` | `#${string}`;
type WithProtocol = `${string}:${string}`;
type Suffix = "" | SearchOrHash;
type SafeSlug = S extends `${string}/${string}`
? never
: S extends `${string}${SearchOrHash}`
? never
: S extends ""
? never
: S;
type StaticRoutes =
| `/next`
| ...;
// comment out union constituents to see exponential impact of dynamic routes
// 6 routes -> 5 -> 4 -> 3
// tsc on M3:
// 45s -> 24s -> 12s -> 6s
// tsgo on M3:
// 9s. -> 5s -> 2.5s -> 0.08s
type DynamicRoutes =
| `/${SafeSlug}/${SafeSlug}/${SafeSlug}`
| `/${SafeSlug}/${SafeSlug}/integrations/${SafeSlug}/${SafeSlug}/resources/${SafeSlug}/${SafeSlug}/billing`
| ...;
type RouteImpl =
| StaticRoutes
| SearchOrHash
| `${StaticRoutes}${SearchOrHash}`
| (T extends `${DynamicRoutes}${Suffix}` ? T : never);
function Link(href: RouteImpl): void {}
Link("/api/ai-playground/sandbox"); // OK
Link("/new/~/integrations/vercel/front/billing"); // OK
```
### 🙁 Actual behavior
Type checking time doubles with every constituent of `DynamicRoutes` with template literals
### 🙂 Expected behavior
Type-checking is reasonably fast
### Additional information about the issue
_No response_
Guía de contribución
Línea de trabajo
Comienza con la reproducción enlazada y el ejemplo de unión de template literals; después compara el comportamiento de tsc en las versiones mencionadas. Mide el tiempo de comprobación de tipos a medida que se añaden constituyentes de DynamicRoutes, incluido el caso de RangeError; se considera terminado cuando se ha resuelto la ralentización exponencial y el ejemplo comprueba sus tipos razonablemente rápido.
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
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Activo
- Claridad
- Bastante claro
- Aptitud para principiantes
- 48/100