Skip to content

Improve type narrowing on switch(true) statements #46600

Closed
@ElianCordoba

Description

@ElianCordoba

Bug Report

🔎 Search Terms

  • switch narrowing
  • switch(true)

🔗 Related issues

Maybe these ones too

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about type guards

⏯ Playground Link

Playground link with relevant code

💻 Code

const char = "(" as string;

// This works fine, char inside the if statements get's property narrowed to type "("
if (char === '(') {
  char;
}

// This doesn't work, char inside the case statement is of type string
switch (true) {
  case char === '(':
    char;
}

🙁 Actual behavior

Char inside the case statement in a switch(true) has a type string

🙂 Expected behavior

It should have type "("

Activity

MartinJohns

MartinJohns commented on Oct 30, 2021

@MartinJohns
Contributor

Duplicate of #37178. Used search terms: switch true in:title

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @MartinJohns@ElianCordoba

        Issue actions

          Improve type narrowing on `switch(true)` statements · Issue #46600 · microsoft/TypeScript