microsoft / microsoft/TypeScript
String union types narrowed to falsy should narrow string to ""
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
Bug Report
🔎 Search Terms
string truthy falsy narrow literal empty
🕗 Version & Regression Information
This is a crashThis 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 😄
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
Reproduce the narrowing case in the linked TypeScript Playground, then read the related discussions in issues #41503, #33878, and #31156. Trace the type-checking behavior for falsy string unions and verify that the narrowed result distinguishes false from the empty string; the issue is done when the regression is covered and the reported type is corrected.
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
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100