microsoft / microsoft/TypeScript

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

Aperta
#63,724 8 commenti 1 reazione 0 assegnatari Vedi su GitHub
Bug Domain: check: Control Flow Help Wanted
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

### 🔎 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_

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia riproducendo l’esempio collegato di TypeScript Playground e confronta il comportamento di Uppercase e Lowercase dopo i confronti con i literal. Traccia il percorso di type narrowing responsabile dell’errore di assegnazione. Il lavoro è completato quando i controlli "BE" | "LU" | "NL" elencati restringono il valore abbastanza da consentire l’assegnazione, considerando anche il comportamento equivalente con le lettere minuscole.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Attiva
Chiarezza
Abbastanza chiara
Idoneità per principianti
52/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.