Skip to content

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

Closed
@alesmenzel

Description

@alesmenzel

πŸ”Ž 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

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptEffort: ModerateRequires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".Fix AvailableA PR has been opened for this issueHelp WantedYou can do this

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions