#Is this a glitch in strictNullChecks?
26 messages · Page 1 of 1 (latest)
no, null and undefined are special-cased to not error for comparisons, so runtime validation like this doesn't error
So strictNullChecks has any effect only on assignments? Isn't that throwing away half its added value?
Isn't that throwing away half its added value?
no, not at all
pretty much everything is type checked like an assignment
direct comparison like this is the only special case afaik
Not sure I understand. Maybe what you call direct comparison is a comparison of values? Note that TS doesn't complain also when I compare types:
function f(arg: string) {
if (typeof(arg) === null)
...
that is also a comparison of values
between "string" and null
and that's one of the issues with this behavior, there's an gh issue for it, one sec
I mean the comparison no longer involves the value of the arg that is marked as string
I have strictNullChecks throughout my project. Is there any way a null could have actually sneaked as an argument to this function?
yes, because ts is not sound
ts can't error or enforce at runtime when types don't line up
Sure, but that goes for all TS types whatsoever
if you interface with any outside interface, you almost definitely have some sort of cast, guard, or parse somewhere
right, but null and undefined are the "empty" values. they can show up much more easily
if you don't have noUncheckedIndexedAccess on and you index out of bounds on an array, for example
you now have an undefined value without a corresponding type
Ok, I see.
or from incorrect assumptions, such as a non-nullish assertion on a String.match