microsoft / microsoft/TypeScript

Type '`x.y.${Name}`' does not satisfy the constraint '`${string}.${Name}`'.(2344)

Đang mở
#61,990 4 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Awaiting More Feedback Suggestion
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 the x.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

https://www.typescriptlang.org/play/?ts=5.9.0-dev.20250702#code/PQKhCgAIUhhB7AtgBwJYBsCmkAurHYCGAzsZgE464AWhVAgpKsZCcagOYB2hARlrniQAFJgAeOTFwAmxAJSQAQgDooMACpCArmQA0kdinQBPSNMwAzVFyK5jyTAC41kAAY572APoBjaph8Aa0gAXkhyTABHLVQI+lJOHn5MTQAedX0AUTEHH0lpAD4Ablc1YHBxZHhKOwdwqJi4hO4+LDSvRnFJGRZFfUUC0NxyLUwi8HAPOoB3asDiAGVrHxTPBZ9yVGQcABYvAGZUgDlCAkguqVkDHE2uDkGwiOjYzHj2FuS0qEgf1zFlAAkAG8TgQAL6uXTfX7A4g3awcMGAkGnTAQ8DFCZTbAWQgYYjHVHnCSXFhw273IZPRqvZpJNrwVLQtz-YzI0FoyHM1yw+F3JHAjnozGTTyQLjwLgAcSkFFQPgA6nMWI8Gi83olWilGdzWcoAF5cn4woHkhFIw0YopAA

💻 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

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. 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

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.