Skip to content

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

Closed

Description

@trusktr

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

Activity

  1. RyanCavanaugh commented on Aug 16, 2019

    @RyanCavanaugh
    Member

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

  2. jack-williams commented on Aug 17, 2019

    @jack-williams
    Collaborator

    Fix here: #32952.

  3. jack-williams commented on Sep 25, 2019

    @jack-williams
    Collaborator
  4. trusktr commented on Dec 6, 2019

    @trusktr
    ContributorAuthor

    Looks like fixed, tested in playground. Thanks!

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

    Experience EnhancementNoncontroversial enhancementsFixedA PR has been merged for this issueSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions