Skip to content

Use before declaration errors when using let statements within a switch case  #19503

Open
@thebanjomatic

Description

@thebanjomatic

TypeScript Version: 2.5.3

Code

function test(value) {
    switch (value) {
        case 1:
            let foo = 10;
            break;
        case 2:
            foo = 20; // Runtime ReferenceError: foo is not defined
            break;
        default:
    }
}

test(2);

tsc --target es2015 (or higher)

Expected behavior:
This should probably produce a compile time error: Block-scoped variable 'pen' used before its declaration. on reference to foo in case 2.

Actual behavior:

The code type-checks and compiles fine, however, when running the es2015 transpiled output, you will receive a ReferenceError error at runtime. Ideally this type of error could be caught at compile time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions