microsoft / microsoft/TypeScript
Type '`x.y.${Name}`' does not satisfy the constraint '`${string}.${Name}`'.(2344)
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 Literal Types, assignability, generics
🕗 Version & Regression Information
- Between versions 4.3.5 and 4.2.3 more cases like this started being supported (
x.${Name}became assignable to${string}.${Name}for example), however thex.y.${Name}case has not bees assignable in any version since template support was added up to and including current nightly ( 5.9.0-dev.20250702 )
⏯ Playground Link
💻 Code
/**
* Compile time assert that A is assignable to (extends) B.
* To use, simply define a type:
* `type _check = requireAssignableTo<T, Expected>;`
*/
export type requireAssignableTo<_A extends B, B> = true;
type worksSinceTypeScript4_3<Name extends string> = requireAssignableTo<
`x.${Name}`,
`${string}.${Name}`
>;
type fails<Name extends string> = requireAssignableTo<
// Type '`x.y.${Name}`' does not satisfy the constraint '`${string}.${Name}`'.(2344)
`x.y.${Name}`,
`${string}.${Name}`
>;
type nonGenericWorks = requireAssignableTo<
`x.y.z`,
`${string}.z`
>;
🙁 Actual behavior
A compile error:
Type '
x.y.${Name}' does not satisfy the constraint '${string}.${Name}'.(2344)
🙂 Expected behavior
No compile error. The .${Name} portion of the type matches exactly, so this can be reduced to checking if "x.y" is assignable to string should be allowed.
Additional information about the issue
I understand that in general not all cases like that are practical to support: perfect assignability checking of template strings is likely infeasible but I decided to report this case as I think this one suggest an algorithm that would be practical to implement and efficient: running a greedy algorithm to remove common constant substrings from the beginning and end of the string, including generic values, might be a viable approach, and would improve handling of this case.
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
Tái hiện trường hợp generic về khả năng gán của template literal đã được báo cáo trong TypeScript Playground được liên kết, đồng thời so sánh với các ví dụ hoạt động x.${Name} và các ví dụ không generic. Điều tra việc kiểm tra khả năng gán của template literal trong compiler và xác minh rằng assertion x.y.${Name} biên dịch thành công mà không làm hồi quy các trường hợp hiện có.
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
- 35/100