π 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
let countryCode: "BE" | "LU" | "NL";
const prefix = "be".toUpperCase() as Uppercase<string>;
if (prefix === "BE" || prefix === "LU" || prefix === "NL") {
countryCode = prefix;
// ^
// Type 'Uppercase<string>' 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<string> or Lowercase<string> 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<string>.
Additional information about the issue
No response
π Search Terms
"narrowing lowercase" "narrowing uppercase"
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?#code/DYUwLgBAxg9grgOzAJwJ4GEYBMQC4IBEAQgKIEQA+hAMgKrlUEBy1BA3AFAewIDOkAB2QgAZgEsAHhAC8hAEYgCAOjAxaAgSGToAhrxAAKAJQQ9EdZuRQ9IADz9kYhAHMAfJw5iREA0NGSZaVliMkoqP3EpIOC6BnDhSMDglgITAG8OCCzoeCQ0TBwZCAjJTgB6MqyAPQ4KrIAVVE0IAHILLWt9exQnNxaIMV4IBBhIPV4xZwQdOVAIVXmmkFaQhhp6SkIUlqVayohG5paHXv7B4dHTXgmpmbmFsCWV0jWCWM3mVh2OAF8gA
π» Code
π Actual behavior
TypeScript cannot narrow down an
Uppercase<string>orLowercase<string>type after explicitly comparing against specific values.π Expected behavior
TypeScript should accept the code above as
"BE" | "LU" | "NL"is strictly a sub-type ofUppercase<string>.Additional information about the issue
No response