microsoft / microsoft/TypeScript
Type narrowing not working correctly with Uppercase<string> & Lowercase<string>
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
### 🔎 Search Terms
"narrowing lowercase" "narrowing uppercase"
### 🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about Common Bugs and Type System Behavior
### ⏯ Playground Link
https://www.typescriptlang.org/play/?#code/DYUwLgBAxg9grgOzAJwJ4GEYBMQC4IBEAQgKIEQA+hAMgKrlUEBy1BA3AFAewIDOkAB2QgAZgEsAHhAC8hAEYgCAOjAxaAgSGToAhrxAAKAJQQ9EdZuRQ9IADz9kYhAHMAfJw5iREA0NGSZaVliMkoqP3EpIOC6BnDhSMDglgITAG8OCCzoeCQ0TBwZCAjJTgB6MqyAPQ4KrIAVVE0IAHILLWt9exQnNxaIMV4IBBhIPV4xZwQdOVAIVXmmkFaQhhp6SkIUlqVayohG5paHXv7B4dHTXgmpmbmFsCWV0jWCWM3mVh2OAF8gA
### 💻 Code
```ts
let countryCode: "BE" | "LU" | "NL";
const prefix = "be".toUpperCase() as Uppercase;
if (prefix === "BE" || prefix === "LU" || prefix === "NL") {
countryCode = prefix;
// ^
// Type 'Uppercase' is not assignable to type '"BE" | "LU" | "NL"'.
// Type 'string' is not assignable to type '"BE" | "LU" | "NL"'.
}
```
### 🙁 Actual behavior
TypeScript cannot narrow down an `Uppercase` or `Lowercase` type after explicitly comparing against specific values.
### 🙂 Expected behavior
TypeScript should accept the code above as `"BE" | "LU" | "NL"` is strictly a sub-type of `Uppercase`.
### Additional information about the issue
_No response_
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先复现链接的 TypeScript Playground 示例,并比较字面量比较之后 Uppercase 和 Lowercase 的行为。跟踪导致赋值错误的类型缩窄路径。当列出的 "BE" | "LU" | "NL" 检查将值缩窄到足以使赋值成功的程度,并同时考虑等效的小写行为时,即视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 活跃
- 描述清晰度
- 基本清楚
- 新手友好度
- 52/100