Skip to content

Exhaustive switch return type does not work when wrapped in a try-catch. #32905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
trusktr opened this issue Aug 14, 2019 · 4 comments
Closed
Labels
Experience Enhancement Noncontroversial enhancements Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Milestone

Comments

@trusktr
Copy link
Contributor

trusktr commented Aug 14, 2019

TypeScript Version: 3.4.5

Search Terms:

Exhaustive switch return type does not work when wrapped in a try-catch

Code

enum Foo {
  One,
  Two,
  Three
}

function test(type: Foo): number {
  switch (type) {
    case Foo.One:
      return 0;
    case Foo.Two:
      return 0;
    case Foo.Three:
      return 0;
  }
}

function test2(type: Foo): number { // ERROR
  try {
    switch (type) {
      case Foo.One:
        return 0;
      case Foo.Two:
        return 0;
      case Foo.Three:
        return 0;
    }
  } catch (e) {
      throw new Error('some error')
  }
}

playground

Expected behavior:

It should still work (because clearly in this case it can only return those values, or throw an error).

Actual behavior:

Error

@RyanCavanaugh RyanCavanaugh added Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript labels Aug 16, 2019
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Aug 16, 2019
@RyanCavanaugh
Copy link
Member

All of the function-always-returning-from-exhaustive-switch forms are special-cased; this one seems like a good additional special case

@jack-williams
Copy link
Collaborator

Fix here: #32952.

@jack-williams
Copy link
Collaborator

@ahejlsberg fixed this in #32695.

@trusktr
Copy link
Contributor Author

trusktr commented Dec 6, 2019

Looks like fixed, tested in playground. Thanks!

@trusktr trusktr closed this as completed Dec 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Experience Enhancement Noncontroversial enhancements Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants