Skip to content

Spurious syntax error for certain switch-case-if scenario #7404

Description

TypeScript Version:

master (a535460), built from source. Also occurs on current Playground.

Code

switch (1) {
case (a): {
    if (c) {}
}
}

Expected behavior:

Error about the unknown names a and c. Compiles to exactly the same JS, since the code is already plain JavaScript.

Actual behavior:

Syntax errors.

tmp.ts(3,12): error TS1005: ';' expected.
tmp.ts(4,1): error TS1005: ':' expected.
tmp.ts(5,1): error TS1128: Declaration or statement expected.

On Playground, you get some more errors (looks like CLI aborts at the syntax errors while Playground continues with type checking).

Type '(a: any) => { if(c: any): any; }' is not assignable to type 'number'.
A function whose declared type is neither 'void' nor 'any' must return a value.

Also, a is documented as “parameter”. So apparently the parser thinks this is a function expression, not a case clause and following block.

This is fairly fragile. Many changes to the code will result in the error vanishing, including:

  • Changing a to 1
  • Changing c to 1
  • Removing the parentheses around a
  • Adding an extra pair of parentheses around a
  • Removing the curly braces of the case block
  • Replacing the empty if block with the empty statement ;
  • Replacing the empty if block with the nonempty statement console.log("foo");
  • Completely removing the if statement

Activity

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

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions