microsoft / microsoft/TypeScript

Type narrowing not working correctly with Uppercase<string> & Lowercase<string>

オープン
#63,724 コメント 8 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Bug Domain: check: Control Flow Help Wanted
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
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_

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、リンクされている TypeScript Playground の例を再現し、リテラル比較後の Uppercase と Lowercase の動作を比較します。代入エラーの原因となっている型の絞り込みの経路を追跡します。記載された "BE" | "LU" | "NL" のチェックによって代入が成功するのに十分な範囲まで値が絞り込まれ、同等の小文字での動作も考慮されていれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
活発
明瞭さ
おおむね明確
初心者へのやさしさ
52/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。