🔎 Search Terms
type, scoping, ifStatement
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about type scoping
⏯ Playground Link
https://www.typescriptlang.org/play/?ts=5.6.2#code/GYVwdgxgLglg9mABMAjACgJSIN4ChEGIzCJpQBOIAphvoYVAJ4AOViAzogLwcUxgBzANx16iCAnZwANlQB00uALQAiFYgCGndhhH1cAX1y5QkWAmQAmTDlFESZSjVtj6TVh269y-YXaP0EmBSsgpKqupaHLqGuEA
💻 Code
functionf1(){if(true)types=string;console.log(""ass);}functionf2(){if(true){types=string;}console.log(""ass);}🙁 Actual behavior
From type-system perspective in the first if it appear that the console.log is part of the body of the if because the type is in scope. However the JS emit is:
functionf1(){if(true);console.log("");}So the console.log is not part of the if body.
🙂 Expected behavior
functionf1(){if(true)types=string;// @ts-expect-error : "cannot find name 's'"console.log(""ass);}Also happy for this to be a parse error. An if statement with only a type declaration as a body hopefully has no use cases.
Additional information about the issue
No response
🔎 Search Terms
type, scoping, ifStatement
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play/?ts=5.6.2#code/GYVwdgxgLglg9mABMAjACgJSIN4ChEGIzCJpQBOIAphvoYVAJ4AOViAzogLwcUxgBzANx16iCAnZwANlQB00uALQAiFYgCGndhhH1cAX1y5QkWAmQAmTDlFESZSjVtj6TVh269y-YXaP0EmBSsgpKqupaHLqGuEA
💻 Code
🙁 Actual behavior
From type-system perspective in the first
ifit appear that theconsole.logis part of the body of theifbecause the type is in scope. However the JS emit is:So the
console.logis not part of the if body.🙂 Expected behavior
Also happy for this to be a parse error. An if statement with only a type declaration as a body hopefully has no use cases.
Additional information about the issue
No response