Skip to content

Bug: Switch case statements allow use of undeclared variables #18720

@kamranayub

Description

@kamranayub

Discovered a bug in my code and was surprised TS didn't catch it for me. It doesn't seem like this is allowed (in Chrome 60, at least).

TypeScript Version: 2.2.4

Code

var baz: number = 2
switch (baz) {
  case 1:
    let foo = 'bar'
    break;
  case 2:
    document.write(foo)
    break;
}

Playground: link

Expected behavior:

TS should warn/error in second case statement that foo is not declared.

Actual behavior:

TS does not throw an error and foo is undefined

If this is expected/by design, I'm interested to know why (and if there's a way to catch it).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions