question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:3
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
RyanCavanaughcommented, Dec 15, 2021

I’m trying to think of a reasonably-efficient algorithm to detect this and not coming up with many good ideas. switch is goto 😕

Update: Have something reasonable

0reactions
DanielRosenwassercommented, Dec 15, 2021

Okay, I misread. Here’s one that would’ve been more obvious to me.

let sHasBeenSet = false;
switch (n) {
    case 0:
        let s = "ok";
        sHasBeenSet = true;
    case 1:
        if (sHasBeenSet) {
            s; // ok by construction
        }
        break;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use a variable name declared with let inside multiple ...
This is what it should be. You declare it before the switch statement. Therefore, the variable is usable in every case beneath the...
Read more >
let - JavaScript - MDN Web Docs
The let declaration declares a block-scoped local variable, optionally initializing it to a value.
Read more >
Why can't variables be initialized in a switch statement?
So, the compiler reports an error that states that they are not sure whether this variable will be used in other cases and...
Read more >
Declaring Variables in Switch Statements | Hampshire UK
You can still declare variables in switch statements, you just have to put curly brackets around the code after the case label.
Read more >
Scoping variables in JavaScript switch statement
This shows that the case statements themselves are not blocks, variables declared within them are hoisted to the switch statement block. When ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found