-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Unsafe access to uninitialized variables is allowed in any function #30053
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
We can't tell this example apart from function blowup() {
ohNo.toLocaleLowerCase();
}
function rescue() {
ohNo = "";
}
let ohNo: string;
rescue();
blowup(); because we don't inline the flow-control effects of functions |
Sure! But then why allow that type declaration in the first place? |
Which part would be theoretically disallowed? |
To make it easy for the compiler (and for the developers who maintain this code! after all, levels of indirection like a function call to initialize the var is just as difficult to follow for a real person), I would say: "If a variable is not initialized immediately after its declaration (not following function calls flow), force it to be declared with So the disallowed thing here would be to declare This would remain valid: let ohYes: string;
if (Math.random() > 0.5) ohYes = 'biiig'
else ohYes = 'nope' |
I'm unclear on the definition of "immediate" here. The The line has to be drawn somewhere between "always allowed" and "always disallowed"; I agree we could shift that line a little bit one way or another, but moving it to fix one example but break others isn't really ideal in terms of not introducing random breaking changes. |
"immediate" as in, direct code statements, not function calls, since functions seem to be the current limitation? |
What I mean is, the We could add a flag but this could also be syntactically enforced by a lint rule, so I'd generally defer to that route, especially since the definition of "immediate" is really up for debate. |
I've been wondering recently what things in Typescript can cause runtime errors even with everything strict, I guess this is one 😱 I expected this to be an error in strict mode, it should be required to be string | undefined if not initialized on the same line.
Any complex initialization logic can be done in a function
|
Isn’t this a duplicate of #23305? |
TypeScript Version: 3.3.1
Search Terms:
uninitialized variable, unsafe access
Code
Expected behavior:
This should not compile when using
strictNullChecks
Actual behavior:
It compiles, probably because the flow analysis abandonned.
This convenience is not worth the safety tradeoff, the compiler should enforce that
ohNo
be typed asstring | undefined
since the initialization code is not immediately following.Playground Link:
https://www.typescriptlang.org/play/#src=function%20blowup()%20%7B%0A%20%20%20%20ohNo.toLocaleLowerCase()%3B%0A%7D%0A%0Alet%20ohNo%3A%20string%3B%0A%0Ablowup()%3B
Related Issues:
Not really. It's somewhat related to the old bugs around class property initialization not being enforced.
The text was updated successfully, but these errors were encountered: