You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Incorrect error of use of unassigned variable #13811
The control flow checker (in strict null checks mode) assures that you'll never accidentally observe undefined in a variable unless the type of the variable allows it. Your program logic is constructed such that undefined is actually a permitted value (at least in parts of the logic), so I'd recommend including undefined in the type of a:
leta: {}|undefined;
The problem with your suggestion is that we'd no longer catch this:
leta: string;// Intended to have assignment here, but forgotif(!a){// Think I have an empty string, but I have undefined}
I think the same argument holds. If the declared type of a variable does not include undefined, the control flow analyzer takes it to mean you never want to observe the variable in an uninitialized state (in any way). I think the consistency of that is important, particularly since you can just add undefined to the type in cases where you want to do those types of checks.
Anders Hejlsberg (@ahejlsberg)
I feel that there is a need for language construct or helper which tests for null or undefined.
Currently I use "if(a) {..}" but if type of 'a' changes to string or number, this test will not be correct.
I feel more safe using this helper now.
functionisNullOrUndefined<T>(obj: T|null|undefined): obj is null|undefined{returntypeofobj==="undefined"||obj===null;}if(!isNullOrUndefined(a)){ ... }
But prefer to have something in the language like:
Adding entirely new statements beyond what JavaScript has is definitely out of scope for TypeScript. To check if a value is nullorundefined in JavaScript, just use x == null. See more discussion here.
Activity
devdoomari commentedon Feb 1, 2017
a has the type of {}, and has no value assigned.
i.e. your code is equivalent to:
so what you wanted:
NN--- commentedon Feb 1, 2017
devdoomari (@devdoomari) It is not what I want.
I want to have it undefined and assign it only if it is undefined.
Full code:
ahejlsberg commentedon Feb 1, 2017
The control flow checker (in strict null checks mode) assures that you'll never accidentally observe
undefinedin a variable unless the type of the variable allows it. Your program logic is constructed such thatundefinedis actually a permitted value (at least in parts of the logic), so I'd recommend includingundefinedin the type ofa:The problem with your suggestion is that we'd no longer catch this:
NN--- commentedon Feb 1, 2017
Anders Hejlsberg (@ahejlsberg) What about this case ?
I would expect it compiling fine since I am checking against undefined.
ahejlsberg commentedon Feb 1, 2017
I think the same argument holds. If the declared type of a variable does not include
undefined, the control flow analyzer takes it to mean you never want to observe the variable in an uninitialized state (in any way). I think the consistency of that is important, particularly since you can just addundefinedto the type in cases where you want to do those types of checks.NN--- commentedon Feb 2, 2017
That's what I ended with.
Thanks.
NN--- commentedon Feb 6, 2017
Anders Hejlsberg (@ahejlsberg)
I feel that there is a need for language construct or helper which tests for null or undefined.
Currently I use "if(a) {..}" but if type of 'a' changes to string or number, this test will not be correct.
I feel more safe using this helper now.
But prefer to have something in the language like:
ahejlsberg commentedon Feb 6, 2017
Adding entirely new statements beyond what JavaScript has is definitely out of scope for TypeScript. To check if a value is
nullorundefinedin JavaScript, just usex == null. See more discussion here.NN--- commentedon Feb 6, 2017
Thanks.
Didn't think about this pattern