microsoft / microsoft/TypeScript

String union types narrowed to falsy should narrow string to ""

Aperta
#45,329 6 commenti 9 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Bug Report

🔎 Search Terms

string truthy falsy narrow literal empty

🕗 Version & Regression Information
  • This is a crash
  • This changed between versions ______ and _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about narrowing
  • I was unable to test this on prior versions because _______
⏯ Playground Link

Playground link with relevant code

💻 Code
let value = Math.random() > 0.5 && "Some Name";

if (!value) {
  value; // Type: false | string, but should be false | ""
}
🙁 Actual behavior

Inside the narrowed block, value should be false | "", because no other string value is falsy.

🙂 Expected behavior

It's the more general false | string instead.

This was a hard issue to search for; #41503, #33878, and #31156 seemed related but not duplicate to me.

Shoutout to RyanCavanaugh for sending me a code snippet that looked like this -- no good deed goes unpunished in the issue tracker 😄

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci il caso di narrowing nel TypeScript Playground collegato, quindi leggi le discussioni correlate nelle issue #41503, #33878 e #31156. Traccia il comportamento del controllo dei tipi per le union di stringhe falsy e verifica che il risultato ristretto distingua false dalla stringa vuota; l’issue è completata quando la regressione è coperta e il tipo riportato è corretto.

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à
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.