Skip to content

Switch doesn't narrow type when type isn't a disc unionΒ #62807

@aryzing

Description

@aryzing

πŸ”Ž Search Terms

discriminated union, switch

πŸ•— Version & Regression Information

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

⏯ Playground Link

https://www.typescriptlang.org/play/?target=99#code/C4TwDgpgBAYg9nKBeKBvKBLAJgLigcgDMF8AaKLAQ2Er3wCNKAnfKAXwCgBjOAOwGdgUYnDzxEKdNjoiyFarQKMW7Dh34B3DMC4ALKAAoRAOmwBKNByhQulftCIk86HgLgAbCMfdwA5gfwAW2o9fAtOaywIQkoAV3dgPGBdJjgNKF4IdIBRJlSmAwAiADlEYJ1dQvIXOPs8ESh+agx+QgwIfgyIADcIJnYzDjYgA

πŸ’» Code

type Foo = { id: 'foo', data: 'bar' }
const foo: Foo = { id: 'foo', data: 'bar' }

switch (foo.id) {
  case 'foo': { console.log('match') }
  default: throw new Error("No match", { cause: foo satisfies never })
//                                                  ^^^^^^^^^
}

πŸ™ Actual behavior

Despite expecting the value to be of type never, it retains its original type.

πŸ™‚ Expected behavior

The value in the default branch should be typed as never.

Additional information about the issue

No response

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

    Issue actions