The strictNullChecks parameter defines whether TypeScript considers null and undefined as distinct types or not. If the parameter is disabled (by default before version 2.0), variables of any type can take on null and undefined values without compilation errors. If enabled (strictNullChecks: true), these values are considered incompatible with other types except where explicitly specified.
Example:
// strictNullChecks: false let name: string = null; // OK // strictNullChecks: true let title: string = null; // Error! let title2: string | null = null; // OK, explicit union type
Enabling strict null checking helps avoid errors at early stages of development when functions/methods do not account for the possibility of receiving null or undefined.
Can a variable of type
numberbe assigned the valueundefinedwhen strictNullChecks is enabled?
Answer: No, if strictNullChecks is enabled, a variable of type number cannot be assigned undefined — only if the type is explicitly declared as number | undefined.
Example:
let count: number = undefined; // TS Error with strictNullChecks: true let count2: number | undefined = undefined; // OK
Story
In a large node.js project, developers disabled strict null/undefined checking for "easing migration". As a result, a year after the launch, one of the API functions returned undefined instead of a numeric value. The client code was not prepared for this, causing the user-side application to crash during a simple computation of response.count + 1.
Story
In an ecommerce project, the product collection came from the server as null, not []. The UI component attempted to map over these products, causing a rendering error. Enabling strictNullChecks immediately highlighted almost 40 similar issues.
Story
In a large library, over time, the number of "allowed" values for some props of the component API became string | null | undefined. This led to a multitude of unhandled situations. After enabling strictNullChecks, it became possible to catch non-obvious bugs related to interface crashes under specific configurations.