Skip to content

Unexpected exhaustive check result for switch statement #29559

Closed
@lriuui0x0

Description

@lriuui0x0

TypeScript Version: [email protected]

Search Terms: switch

Code

const assertUnreachable = (x: never): void => {}

const f = (x: 'a' | 'b') => {
  switch (x) {
    case 'a':
      {
      }
      break;

    case 'a':
      {
      }
      break;
  }
};

const g = (x: { t1: 'a'; t2: 'a1' | 'a2' } | { t1: 'b'; t2: 'b1' | 'b2' }) => {
  switch (x.t1) {
    case 'a':
      {
        switch (x.t2) {
          case 'a1':
            {
            }
            break;

          case 'a2':
            {
            }
            break;

          default: {
            assertUnreachable(x.t2);
          }
        }
      }
      break;
  }
};

Expected behavior:

Function f should not pass the typecheck at the second case block.
Function g should pass the typecheck at assertUnreachable.

Actual behavior:

Function f passes the typecheck at the second case block.
Function g doesn't pass the typecheck at assertUnreachable.

Note that if we put assertUnreachable in function f, it will work. So this seems to be only happening for the nested switch.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions