Sub topic 5
Equality Checks and Null/Undefined Handling
Prerequisites and the narrowing model
Assume you know basic TypeScript syntax, union types, if statements, and switch statements. This chapter focuses on equality checks that help TypeScript choose a more specific type inside a branch.
A union type describes a value that can have more than one type. For example, string | null | undefined means the value may be a string, null, or undefined. TypeScript uses control flow analysis to track checks and remove impossible members from the union.
With strictNullChecks enabled, null and undefined are separate types. You must handle them before using a value as though it exists.
function greet(name: string | null | undefined): string {
if (name == null) {
return "Hello, stranger";
}
return `Hello, ${name.toUpperCase()}`;
}After the if statement, name is known to be a string.
Strict equality narrows values
The === operator checks both value and type. TypeScript can use it to remove union members that cannot satisfy the comparison.
function describe(value: string | number): string {
if (value === "ready") {
return "The string is ready";
}
if (typeof value === "string") {
return `Text: ${value}`;
}
return `Number: ${value}`;
}In the first branch, value must be the string literal "ready". The second branch handles the remaining strings. The final branch contains only numbers.
Equality can also compare two variables. When both variables share a possible type, a successful comparison narrows both variables to that shared type.
function sameValue(
left: string | number,
right: string | number
): string {
if (left === right) {
return `They match: ${left}`;
}
return "They differ";
}Inside the true branch, TypeScript knows that both values have a compatible type. The exact type may still be string | number, because two strings or two numbers can match.
Handling null and undefined
Use an explicit check when you want to distinguish the cases:
function readLabel(label: string | null | undefined): string {
if (label === null) {
return "Label was deliberately cleared";
}
if (label === undefined) {
return "Label was not provided";
}
return label.toUpperCase();
}The expression value == null is a special, common pattern. It matches only null or undefined, so it removes both from the type. This is one of the few useful cases for loose equality.
The opposite check also narrows the value:
function printLabel(label: string | null | undefined): void {
if (label != null) {
console.log(label.toUpperCase());
}
}Here, label is a string inside the branch.
Equality checks and falsy values
Do not replace a null check with a truthiness check when valid values may be empty or zero.
function formatCount(count: number | null): string {
if (count === null) {
return "No count";
}
return `Count: ${count}`;
}A check such as if (!count) also rejects 0, even though zero is a valid number. Similarly, if (!text) rejects the empty string. Use === null, === undefined, or != null when those values need precise meaning.
Exercise
Exercise
Why is count === null safer than if (!count) when count has type number | null?
Show answer
if (!count) treats both null and 0 as false, while count === null rejects only null and preserves zero as a valid number.