-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
simulate scope at let to a if statement but failed #36853
Comments
The new issue template asks for a Playground link https://www.typescriptlang.org/play/ where your issue is reproduced. The above code has several errors due to missing declarations, but not the one you're reporting. Please consider cleaning it up if you don't want this closed as unactionable. Good luck! |
@jcalz thank you for you commented, it is clean up now |
Duplicate of #9998. |
I don't know about #9998; the function here is an IIFE which, IIRC (ha ha), is inlined: let z: string;
(() => { z = "a" })()
z.toUpperCase(); // okay Instead I think it has to do with trying to "save" the results of CFA in variables, return them from functions, etc., and then use them later... which doesn't work, as in #12184, #20497, #24865 (and probably others): let a: string;
const bool = Math.random() < 0.5;
if (bool) { a = ""; }
if (!bool) { a = ""; }
a.toUpperCase(); // error In any case, a mcve for this probably looks like: let b: string;
if ((() => {
if (Math.random() < 0.5) return true;
b = "";
return false;
})()) {
b = "";
}
b.toUpperCase(); // error |
We don't have any kind of mechanisms capable of creating inferences like "This function either returns |
TypeScript Version: 3.7.5
Search Terms: if-let, scope, closure
Expected behavior: tsc report no error
Actual behavior: tsc report "Variable 'a' is used before being assigned."
Related Issues:
Code
if-let statement is easy to control scope in golang
I try to use closure to simulate it, because ts can not use if-let statement. tsc reported error "Variable 'a' is used before being assigned."
Output
Compiler Options
Playground Link: Provided
The text was updated successfully, but these errors were encountered: