TSConfig
strictNullChecks
When strictNullChecks
is false
, null
and undefined
are effectively ignored by the language.
This can lead to unexpected errors at runtime.
When strictNullChecks
is true
, null
and undefined
have their own distinct types and you’ll get a type error if you try to use them where a concrete value is expected.
For example with this TypeScript code, users.find
has no guarantee that it will actually find a user, but you can
write code as though it will:
tsTry
declare constloggedInUsername : string;constusers = [{name : "Oby",age : 12 },{name : "Heera",age : 32 },];constloggedInUser =users .find ((u ) =>u .name ===loggedInUsername );console .log (loggedInUser .age );
Setting strictNullChecks
to true
will raise an error that you have not made a guarantee that the loggedInUser
exists before trying to use it.
tsTry
declare constloggedInUsername : string;constusers = [{name : "Oby",age : 12 },{name : "Heera",age : 32 },];constloggedInUser =users .find ((u ) =>u .name ===loggedInUsername );'loggedInUser' is possibly 'undefined'.18048'loggedInUser' is possibly 'undefined'.console .log (. loggedInUser age );
The second example failed because the array’s find
function looks a bit like this simplification:
ts
// When strictNullChecks: truetype Array = {find(predicate: (value: any, index: number) => boolean): S | undefined;};// When strictNullChecks: false the undefined is removed from the type system,// allowing you to write code which assumes it always found a resulttype Array = {find(predicate: (value: any, index: number) => boolean): S;};