microsoft / microsoft/TypeScript
Type narrowing not working correctly with Uppercase<string> & Lowercase<string>
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
### 🔎 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_
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the linked TypeScript Playground example and compare the behavior of Uppercase and Lowercase after literal comparisons. Trace the type-narrowing path responsible for the assignment error. Done means the listed "BE" | "LU" | "NL" checks narrow the value enough for the assignment to succeed, with equivalent lowercase behavior considered.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100