Skip to content

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

@trusktr

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

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