microsoft / microsoft/TypeScript
type checking complexity with multiple template literals in unions
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.3k
- Merge moyen
- 2 j 4 h
- PR mergées (30 j)
- 132
Description
### 🔎 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_
Guide de contribution
Ouvrir le guide de contribution
Piste de recherche
Commencez par la reproduction liée et l’exemple d’union de template literals, puis comparez le comportement de tsc entre les versions mentionnées. Mesurez le temps de vérification des types à mesure que des constituants de DynamicRoutes sont ajoutés, y compris le cas RangeError ; le travail est terminé lorsque le ralentissement exponentiel est résolu et que l’exemple est vérifié par le système de types suffisamment rapidement.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- typescript
- Domaine
- compilers, performance
- Type d'issue
- Bug
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- Active
- Clarté
- Plutôt claire
- Accessibilité débutants
- 48/100