How to enforce strict null checks in TypeScript ?
Last Updated :
23 Feb, 2022
In Typescript to enforce strict null checks in tsconfig.json file, we need to enable "strictNullChecks" to true. When "strictNullChecks" is false, the language generally ignores variables of type null and undefined. If null and undefined is used in places where a definite value is expected, it raises an error. When "strictNullChecks" is true when null and undefined is used a type error gets raised.
Example 1: Assigning null and undefined to string type variable
A null value is passed to a string-type variable.
JavaScript
Output:
When strict null checks are enabled.

If strict null checks are not enabled:

If undefined is given to a variable and strictnullchecks is true:
JavaScript
let var1: string = undefined;
Output:

Example 2: Referencing variables before assigning values
When strictnullchecks are enabled variables that are not assigned any value cannot be referenced. The only exception is when the variable is of type "undefined", those variables can be referenced before being assigned by any value.Â
JavaScript
let var1: string | number;
let var2: string | null;
let var3: string | undefined;
// Throws error
var1;
// Throws error
var2;
// Doesn't Throws error
var3;
var1 = 1;
var2 = "abc";
// No error
var1;
// No error
var2;
Output: This is how it looks like in code editor.

Example 3: Covering strictnullchecks
The below code is a small example of strictnullchecks. An interface is created and ?: is used while declaring variable age, it says the variable can be null or identified. As strict null checks are enabled when we try to display student.age it gives us warning. When it's further confirmed that student.age is not null no warning or error is given.Â
JavaScript
interface Student {
name: string;
age?: number;
}
function displayStudentInfo(student: Student) {
// Error TS2532: Object is possibly 'undefined'.
console.log(student.name + ` , ${student.age.toString()}`);
// Confirming that student.age is not null
console.log(student.name + ` , ${student.age!.toString()}`);
// Condition is checked that student's age isn't null
if (student.age != null) {
console.log(student.name + ` , ${student.age.toString()}`);
}
}
let obj: Student = { name: "sean", age: 20 };
console.log(displayStudentInfo(obj));
Output:Â
sean , 20
sean , 20
sean , 20
Similar Reads
How to declare nullable type in TypeScript ? In vanilla JavaScript, there are two primary data types: null and undefined. While TypeScript previously did not allow explicit naming of these types, you can now use them regardless of the type-checking mode. To assign undefined to any property, you need to turn off the --strictNullChecks flag. How
2 min read
How to check interface type in TypeScript ? Typescript is a pure object-oriented programming language that consists of classes, interfaces, inheritance, etc. It is strict and it statically typed like Java. Interfaces are used to define contacts in typescript. In general, it defines the specifications of an entity. Below is an example of an in
2 min read
How to Check if an Object is Empty in TypeScript ? In TypeScript, it's common to encounter scenarios where you need to determine if an object is empty or not. An empty object typically means it contains no properties or all its properties are either undefined or null. Below are the methods to check if an object is empty or not in TypeScript: Table o
3 min read
How to Check Types in Typescript? Checking types in TypeScript involves methods like typeof for primitive types, instanceof for class instances, and custom type guards for complex type validation. These techniques help ensure variables are correctly typed, improving code safety, and readability, and preventing runtime errors.Here ar
3 min read
How to check null and undefined in TypeScript ? In this article let's learn how to check if a variable is null or undefined in TypeScript. A variable is undefined when it's not assigned any value after being declared. Null refers to a value that is either empty or doesn't exist. null means no value. To make a variable null we must assign null val
3 min read