Skip to content

Narrowing switch(true) doesnt work for default case and is wrong for fallthrough cases in 5.3 #55986

Closed

Description

🔎 Search Terms

switch, fallthrough

🕗 Version & Regression Information

  • This is a crash No
  • This changed between versions 5.2 and 5.3

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.3.0-dev.20231004#code/C4TwDgpgBAZg9nKBeKByAhqqAfNAjLXVAY1QCgzi4A7AZ2FmTUynVtgQtoHcBLYYgAsAFMABOAVwgBKKAG8yUJVGJtoMZEhQZUALkXLVtdZu0F9ywzVpwANhAB0tuAHNhMaQcsB6b5f9QAHoA-F7KeGIQ6ADWYQAmEDDoErbAFv5UdHaOzm4eYUq+AZYhZAC+QA

💻 Code

type foo = 'a' | 'b' | 'c'

const f = 'a' as foo

switch(true) {
    case f === 'a':
    case f === 'b':
      console.log(f)
      //          ^?  'b'
      break
    default:
      console.log(f)
      //          ^?  'a' | 'b' | 'c'
}

🙁 Actual behavior

The fallthrough case has wrong type b.
The default case is not narrowed.

🙂 Expected behavior

In the fallthrough the type should be a union of a | b.
The default case should be c.

Additional information about the issue

Related to 70b7de1

Activity

  1. DanielRosenwasser commented on Oct 5, 2023

    @DanielRosenwasser
    Member

    Bizarre that it might have something to do with empty case clauses

    Playground Link

    type foo = 'a' | 'b' | 'c'
    
    const f: foo = 'a' as any
    
    switch(true) {
        case f === 'a':
          console.log(f)
          //          ^? "a"
          // falls through 
        case f === 'b':
          console.log(f)
          //          ^? "a" | "b"
          break
        default:
          console.log(f)
          //          ^? foo
    }
    
  2. added
    Effort: Moderate
    on Oct 5, 2023
  3. jakebailey commented on Oct 5, 2023

    @jakebailey
    Member

    Mateusz Burzyński (@Andarist) if he beats me to it before tomorrow

  4. fatcerberus commented on Oct 5, 2023

    @fatcerberus

    Daniel Rosenwasser (@DanielRosenwasser) fwiw while your code narrows correctly for the cases, it still gets default wrong (it should narrow to "c")

    Also when did switch (true) start working? (this pattern is an irrational pet peeve of mine but that's neither here nor there)

  5. DanielRosenwasser commented on Oct 5, 2023

    @DanielRosenwasser
    Member

    I also think it's a little hokey but #53681

  6. jakebailey commented on Oct 5, 2023

    @jakebailey
    Member

    This comes down to the fact that the PR didn't handle multiple clauses at all... I should have known the PR was too simple 😅

    I have a fix for the multiple clause cases, but default, not so sure about. May need to just be unnarrowed there as I'm not sure we have a mechanism to narrow like we do the other switch case instances as those do the narrowing directly without control flow, whereas this has to be more like if statements or something. I guess we'll have something like this eventually for matching...

  7. added
    Fix Available
    on Oct 5, 2023
  8. jakebailey commented on Oct 5, 2023

    @jakebailey
    Member

    PR at #55991.

  9. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugEffort: ModerateFix Available

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions