microsoft / microsoft/TypeScript

type checking complexity with multiple template literals in unions

Offen
#63,342 5 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Domain: check: Big Unions Experimentation Needed Possible Improvement
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

### 🔎 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_

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Beginne mit der verlinkten Reproduktion und dem Template-Literal-Union-Beispiel und vergleiche anschließend das Verhalten von tsc über die genannten Versionen hinweg. Miss die Zeit der Typprüfung, während DynamicRoutes-Bestandteile hinzugefügt werden, einschließlich des RangeError-Falls; abgeschlossen ist die Aufgabe, wenn die exponentielle Verlangsamung behoben ist und das Beispiel in angemessener Zeit typgeprüft wird.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers, performance
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Aktiv
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
48/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.