microsoft / microsoft/TypeScript
Bug: Circular references not allowed for template literal types
Đang mở
Chưa có ai nhận issue này.
Awaiting More Feedback
Suggestion
- Ngôn ngữ chính
- Go
- Star
- 111k
- Fork
- 14.4k
- Merge trung bình
- 1 ngày 19 giờ
- Pull request đã merge (30 ngày)
- 117
Mô tả
Bug Report
🔎 Search Terms
- circularly, template literal
Related issues: #43335.
🕗 Version & Regression Information
- This changed between versions 4.0.5 and 4.1.5 (different bug before)
⏯ Playground Link
Playground link with relevant code
💻 Code
// you can have circular references in tuple types
type OneOrMoreArr0<C> = C | [C, ...OneOrMoreArr0<C>[]];
// note that
//
// type OneOrMoreArr0<C> = C | [C, OneOrMoreArr0<C>];
//
// is also valid
type OneOrMoreArr<C> = Exclude<OneOrMoreArr0<C>, C>;
const noEmptyArr: [] extends OneOrMoreArr<'*'> ? false : true = true;
const x1: OneOrMoreArr<'*'> = ['*'];
const x2: OneOrMoreArr<'*'> = ['*', '*'];
const x3: OneOrMoreArr<'*'> = ['*', '*', '*']
// but when you try and do the same thing with template literal types, you get an error
//
// "Type alias 'OneOrMoreStr0' circularly references itself."
//
// "Type 'OneOrMoreStr0' is not generic."
type OneOrMoreStr0<C extends string> = C | `${C}${OneOrMoreStr0<C>}`
// we should be able to do the above, and then have e.g.,
//
// const y1: OneOrMoreStr<'*'> = '*';
// const y2: OneOrMoreStr<'*'> = '**';
// const y3: OneOrMoreStr<'*'> = '***';
// if 'C' is the empty string, the above just reduces to:
//
// OneOrMoreStr0<''> = '';
//
// otherwise the same logic as with `OneOrMoreArr0` should apply
🙁 Actual behavior
You get the error:
Type alias 'OneOrMoreStr0' circularly references itself.
Type 'OneOrMoreStr0' is not generic.
🙂 Expected behavior
This should type check like with the tuple example - it's the same principle, just with template literals.
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 TypeScript Playground được liên kết và các ví dụ rút gọn về tuple đệ quy và template literal trong issue. So sánh cách các bí danh tuple đệ quy được chấp nhận với cách bí danh template literal bị từ chối; hoàn tất khi các ví dụ OneOrMoreStr kiểm tra kiểu thành công mà không có các lỗi tham chiếu vòng.
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
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 38/100