#Is this a glitch in strictNullChecks?

26 messages · Page 1 of 1 (latest)

warm ravine
#
function f(arg: string) {
    if (arg === null) 
...

Shouldn't strictNullChecks=true have caused TS to complain that the comparison is redundant?

latent delta
#

no, null and undefined are special-cased to not error for comparisons, so runtime validation like this doesn't error

warm ravine
#

So strictNullChecks has any effect only on assignments? Isn't that throwing away half its added value?

latent delta
#

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

warm ravine
#

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) 
...
latent delta
#

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

warm ravine
#

I mean the comparison no longer involves the value of the arg that is marked as string

latent delta
#

that part doesn't matter

#

any === null or === undefined is allowed

latent delta
warm ravine
#

I have strictNullChecks throughout my project. Is there any way a null could have actually sneaked as an argument to this function?

latent delta
#

yes, because ts is not sound

#

ts can't error or enforce at runtime when types don't line up

warm ravine
#

Sure, but that goes for all TS types whatsoever

latent delta
#

if you interface with any outside interface, you almost definitely have some sort of cast, guard, or parse somewhere

latent delta
#

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

warm ravine
#

Ok, I see.

latent delta
#

or from incorrect assumptions, such as a non-nullish assertion on a String.match

warm ravine
#

Thanks!

#

!resolve