-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Description
π Search Terms
Bug Report
π Search Terms
- switch case let variable scope
- cross-case variable access
- TypeScript let block scoping in switch statements
- case branch variable visibility
βοΈ Bug Description
Unexpected scoping behavior occurs with let-declared variables in different case branches of a switch statement: a let variable declared in case 1 is accessible in case 2, and the TypeScript compiler fails to throw a compilation error for this cross-branch variable access, leading to potential runtime errors.
π Code Reproduction
class A {
value: string | undefined;
}
function test(param: any, flag: number) {
switch (flag) {
case 1:
// let variable declared in case 1 branch
let jsonValue = (param as A).value;
console.log(jsonValue);
break;
case 2:
let jsonValues = (param as A).value;
// Error: Accessing jsonValue (declared in case 1) - no TypeScript compilation error
if (jsonValue != undefined) {
console.log(jsonValues);
}
break;
default:
console.log("1111");
}
}
function test1(aaa: string) {
console.log(aaa);
}
let a = new A();
test(a, 2);The TypeScript compiler should detect the access to the undeclared variable jsonValue in the case 2 branch and throw a compilation error (e.g., Cannot find name 'jsonValue'), adhering to the block-scoping rules of let.
π Version & Regression Information
- This changed between versions ______ and _______
- This changed in commit or PR _______
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
- I was unable to test this on prior versions because _______
β― Playground Link
No response
π» Code
// Your code here
class A {
value: string | undefined;
}
function test(param: any, flag: number) {
switch (flag) {
case 1:
// let variable declared in case 1 branch
let jsonValue = (param as A).value;
console.log(jsonValue);
break;
case 2:
let jsonValues = (param as A).value;
// Error: Accessing jsonValue (declared in case 1) - no TypeScript compilation error
if (jsonValue != undefined) {
console.log(jsonValues);
}
break;
default:
console.log("1111");
}
}
function test1(aaa: string) {
console.log(aaa);
}
let a = new A();
test(a, 2);π Actual behavior
he TypeScript compiler does not throw any compilation errors, and the code compiles successfully. However, a runtime error ReferenceError: jsonValue is not defined is thrown when flag=2.
π Expected behavior
The TypeScript compiler should detect the access to the undeclared variable jsonValue in the case 2 branch and throw a compilation error (e.g., Cannot find name 'jsonValue'), adhering to the block-scoping rules of let.
Additional information about the issue
No response