microsoft / microsoft/TypeScript

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

Open
#19,503 9 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: Binder Effort: Difficult
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided switch-case example using TypeScript 2.5.3 and compile it with --target es2015 or higher. Trace the type-checking path for block-scoped let declarations and references across case clauses. Done means the reference to foo in case 2 produces a compile-time block-scoped-variable-before-declaration diagnostic instead of compiling to a runtime ReferenceError.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.