-
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
Variable is used before assigned, but we're checking whether it's been assigned #12916
Comments
A dupe of #9568? |
Looks like it. Although the comments in #9568 make it sound like the second example here would have a compile error (as it too would require |
IMHO the second example should not require |
I just came across this issue in our code when trying to switch to --strictNullChecks. IMHO it might even be valid to not throw this error on variable usage in any conditional checks (as long as you don't do any property accesses on it) as a non defined variable would always be undefined the check would be valid at runtime. Another question is if
would be legitimate or rather confusing. In any way I think it is a valid use-case to not set @gcnew obviously strict checking |
@Aides359 From my point of view allowing it is strictly a bad thing. You've declared the type of the variable to be PS: if you want dynamic/inferred behaviour, just skip the type annotation. Compilers should enforce what they are told. A compiler that doesn't is both confusing and unreliable. |
@gcnew well if you follow that strictly then In any case, I think the stated code blocks are a valid and probably commonly used logic construct and I on the contrary would be rather disappointed if my compiler would force me to generally allow a type it won't have and shouldn't be allowed at the end of the day or disregard typing at all (which defeats the point of Typescript). What I just realized:
which might be just good enough. However I still think it would be nice to allow just the number type on x and check for undefined just to be more explicit in stating intent (since from a function flow perspective the problem would be the undefined assignment, not the return). |
The compiler does not allow you to read from it unless it's 100% sure that you've already assigned to the variable. Implicit unassigned value and the type of a well initialised variable are two distinct things. The variable has a single type and you must conform to it in all your code. Making sure the variable is well assigned is the job for the Control Flow Analyser (CFA). |
Check #9568 again. It has some good points and it was agreed there that such a behaviour is questionable and confusing. |
Here's a pattern people might use:
This pattern consists of:
let x: number;
Currently, we can't compile this function. We get an error for
!x
sayingVariable 'x' is used before being assigned.
This could be fixed by #11463, but
if (!x!)
is ugly...This can also be fixed with
let x: number | undefined;
, but that is unintuitive since we don't normally require| undefined
, as in the following, which compiles fine:CC @sandersn
The text was updated successfully, but these errors were encountered: