microsoft / microsoft/TypeScript

Switching on a template literal expression does not narrow the interpolated union variable

Đang mở
#64,087 2 bình luận 5 reaction 0 người được giao Xem trên GitHub

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

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, template expression, switch, narrowing, control flow analysis, interpolated variable, union, as const

### 🕗 Version & Regression Information

This is the behavior in every version tried (4.1.5, 5.9.2, 7.0.2; not expressible before 4.1), and I (had claude) review the FAQ for entries about type narrowing and template literal types

### ⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.9.2#code/CYUwxgNghgTiAEYD2A7AzgF3gMyUgXPAERQAOpEIR8APsaSLNXUQEZQodREDcAUHzQB3AJYYwAC3gAKAAYASAN64kAX3ikRIWfChpEqTAEp4ivvAuI9CEuUoatRfOcuvk6LGQohCt79QBeHDx+V1cAenD4AD0AfngQAA8GMAwQYGCkeAwszhgYJCFsrL9KXl1UgFcoCAgAT3hgJBB9FCQsPIKhFwtWOCgAa35VAUiDFAwCiEJhMUkRFABzeFRMxpE4VPr4TsL9PQTk8DTgQVFxKWkVEzM3a2IvMucwy3dMXTsfB8-AzNDLPqMIZ8EZAA

### 💻 Code

```ts
declare const foo: "apple" | "pear" | "banana";

switch (`${foo} pie` as const) {
case "apple pie":
const apple: "apple" = foo;
// ^? expected foo to narrow to "apple"; actually does not narrow
break;
}

// control: switching on foo directly narrows as expected
switch (foo) {
case "apple":
const apple: "apple" = foo;
break;
}
```

### 🙁 Actual behavior

Inside `case "apple pie":`, `foo` is still typed `"apple" | "pear" | "banana"`, so `const apple: "apple" = foo` fails with:
```
error TS2322: Type '"apple" | "pear" | "banana"' is not assignable to type '"apple"'.
```

This is wrong because the case can only be reached when `foo === "apple"`. The switch subject `${foo} pie as const` is typed as `"apple pie" | "pear pie" | "banana pie"`, and each case label corresponds to exactly one value of foo. The compiler already computes that correspondence, but control flow analysis doesn't propagate the match back to `foo`. The equivalent `if ((${foo} pie as const) === "apple pie")` fails the same way, while switching on foo directly narrows as expected.

### 🙂 Expected behavior

`foo` should narrow to `"apple"` inside case `"apple pie"`, and correspondingly in the other cases.

Since `foo`'s type is a finite union of string literals, `${foo} pie` evaluates to a distinct literal for each member. The mapping from case label back to foo's value is unambiguous and already known to the checker, which is what makes the subject type `"apple pie" | "pear pie" | "banana pie"` in the first place.

Matching a case label should therefore narrow `foo` the same way switch (foo) does, analogous to how narrowing already works backwards through other expression forms like `typeof foo === "string"` and `switch (true)` (TS 5.3)

### Additional information about the issue

I understand this may be a design limitation rather than a bug, since narrowing generally only tracks references and the switch subject here is an expression. But the checker already computes the exact one-to-one mapping from `foo`'s members to the subject's members when it constructs the type `"apple pie" | "pear pie" | "banana pie"`, so the information needed to narrow is present.

Related but distinct issues found while searching: #46045, #53887, #42007, #20949, #51426 (none cover narrowing the interpolated variable of a template literal expression used as a switch/equality subject).

This issue was drafted using the assistance of LLMs (claude), though everything was read, directed, and reviewed by me before submitting this issue through the github website.

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

Bắt đầu với reproduction được liên kết trong TypeScript Playground và so sánh hành vi của template-literal switch và phép so sánh bằng với switch trực tiếp trên foo. Truy vết đường đi narrowing union và template-literal hiện có của checker; hoàn tất khi foo được narrowing thành literal tương ứng trong từng case mà không làm suy giảm điều khiển của switch trực tiếp.

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
Sôi nổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
45/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.