microsoft / microsoft/TypeScript
type checking complexity with multiple template literals in unions
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Go
- Star
- 111k
- Fork
- 14.3k
- Merge trung bình
- 2 ngày 4 giờ
- Pull request đã merge (30 ngày)
- 132
Mô tả
🔎 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
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu với bản tái hiện được liên kết và ví dụ về phép hợp template literals, sau đó so sánh hành vi của tsc giữa các phiên bản được đề cập. Đo thời gian kiểm tra kiểu khi các thành phần của DynamicRoutes được thêm vào, bao gồm cả trường hợp RangeError; được coi là hoàn thành khi hiện tượng chậm theo cấp số nhân đã được giải quyết và ví dụ được kiểm tra kiểu với tốc độ hợp lý.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- typescript
- Lĩnh vực
- compilers, performance
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Sôi nổi
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 48/100