Skip to content

Null and Undefined

In one line

null and undefined are distinct types in TypeScript. With strictNullChecks enabled, they are not assignable to other types, preventing null reference errors.

In simple words

In TypeScript, null and undefined are separate types. With strictNullChecks (part of strict mode), they are not implicitly assignable to other types — you must explicitly include them in unions: string | null | undefined.

This prevents the most common runtime error: accessing a property on null or undefined. TypeScript forces you to check for null before using a value that might be null, using if checks, optional chaining (?.), or nullish coalescing (??).

Without strictNullChecks, null and undefined are assignable to any type, which defeats type safety. Always enable strict mode in new projects.

Example

typescript
let name: string | null = null;
// name.toUpperCase(); // Error: possibly null

if (name !== null) {
  name.toUpperCase(); // OK: narrowed to string
}

// Or use optional chaining
name?.toUpperCase(); // returns undefined if name is null

strictNullChecks forces you to handle null before using the value.

Related terms

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary