Skip to content

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

Open
@JoshuaKGoldberg

Description

@JoshuaKGoldberg

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 😄

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions