Why I am not getting warnings about StrictNullChecks in typescript Why I am not getting warnings about StrictNullChecks in typescript typescript typescript

Why I am not getting warnings about StrictNullChecks in typescript


Turn on "strict": true, in the tsconfig.json to enable this warning.

Or if you don't want all the strict options you in need:

"strictNullChecks": true,"strictPropertyInitialization": true,

See the documentation for more information

--strictNullCheck:

In strict null checking mode, the null and undefined values are not in the domain of every type and are only assignable to themselves and any (the one exception being that undefined is also assignable to void).

--strictPropertyInitialization

Ensure non-undefined class properties are initialized in the constructor. This option requires --strictNullChecks be enabled in order to take effect.

The second one is the one you want (but needs strictNullChecks to work)

Btw, as @jayasai amerineni mentions, your example shouldn't trigger this warning.


strictNullChecks checks that all the operations performed on a property leads to a non-null value. But in the bar function it is only printing the person.age regardless if it is null or undefined. If you were to say person.age.toString() typescript would throw a compile time error.


I was having a similar problem, but I was consuming a possibly null type as a argument in a function which required a non-null parameter, so it definitely should have been throwing an error for me.

I had already turned on strict: true and strictNullChecks: true for good measure, but it only actually worked for me when I closed VSCode and reloaded it. If anyone else has this issue, makes sure to restart your code editor!