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.