-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Use before declaration errors when using let statements within a switch case #19503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The current algorithm we use to detect use before def checks locations of declarations in the source file. this works in most cases, but not in cases where control flow is involved. |
In case anyone has the same issue: this bug also affects classes and enums declared in a preceding case clause. |
I've found a case that is related to this bug, but TS only catch it when strictNullChecks is true code on playground detected when not detected when
|
Real world example of this causing a bug (the commit is the fix): microsoft/vscode@7e266b2
|
I'm trying to think of a reasonably-efficient algorithm to detect this and not coming up with many good ideas. Update: Have something reasonable |
So the only caveat here is that we'll have unavoidable false positives: let n = 0;
switch (n) {
case 0:
let s = "ok";
case 1:
if (n === 0) {
s; // ok by construction
}
break;
} not clear what to do about that... |
If I understand correctly, for a switch (100) {
case 99:
let x;
case 100:
x = 0;
console.log(x);
} |
@DanielRosenwasser the problem is that the code runs without error in the |
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;
} |
TypeScript Version: 2.5.3
Code
Expected behavior:
This should probably produce a compile time error:
Block-scoped variable 'pen' used before its declaration.
on reference to foo incase 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.
The text was updated successfully, but these errors were encountered: