microsoft / microsoft/TypeScript

type checking complexity with multiple template literals in unions

Aperta
#63,342 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Domain: check: Big Unions Experimentation Needed Possible Improvement
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

🔎 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 <Link /> components.

// Example type definitions for Next.js routes

type SearchOrHash = `?${string}` | `#${string}`;
type WithProtocol = `${string}:${string}`;

type Suffix = "" | SearchOrHash;

type SafeSlug<S extends string> = 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<T extends string = string> =
  | `/${SafeSlug<T>}/${SafeSlug<T>}/${SafeSlug<T>}`
  | `/${SafeSlug<T>}/${SafeSlug<T>}/integrations/${SafeSlug<T>}/${SafeSlug<T>}/resources/${SafeSlug<T>}/${SafeSlug<T>}/billing`
  | ...;

type RouteImpl<T> =
  | StaticRoutes
  | SearchOrHash
  | `${StaticRoutes}${SearchOrHash}`
  | (T extends `${DynamicRoutes<infer _>}${Suffix}` ? T : never);

function Link<RouteType>(href: RouteImpl<RouteType>): 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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con la riproduzione collegata e l’esempio di unione di template literals, quindi confronta il comportamento di tsc nelle versioni indicate. Misura il tempo di type-checking man mano che vengono aggiunti i costituenti di DynamicRoutes, incluso il caso RangeError; il lavoro è completato quando il rallentamento esponenziale è stato risolto e l’esempio viene sottoposto al type-checking in tempi ragionevoli.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers, performance
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Attiva
Chiarezza
Abbastanza chiara
Idoneità per principianti
48/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.