Skip to content

Null and Undefined in TypeScript

Handle null and undefined safely with strictNullChecks, union types, and definite assignment.

What you'll learn

  • The difference between null and undefined in TypeScript
  • How strictNullChecks prevents null-related bugs
  • Using union types (T | null) to model absence
  • Definite assignment assertion for late-initialized properties

Concept

Two Kinds of Nothing

JavaScript has two empty values: null (an intentional absence) and undefined (an uninitialized variable or missing property). TypeScript treats them as distinct types. By default (without strictNullChecks), null and undefined are assignable to any type, which is dangerous — you can assign null to a string variable and get a runtime error later. With strictNullChecks enabled, you must explicitly opt into nullability with a union: string | null.

strictNullChecks

This is the single most important compiler flag. When enabled, null and undefined are not assignable to other types. A variable typed string cannot hold null. To allow null, you write string | null. This forces you to handle the null case before using the value, eliminating the infamous "cannot read property of null" runtime error. Most modern TypeScript projects enable this flag via strict: true in tsconfig.

Definite Assignment

Sometimes a property is initialized after construction (e.g. by a framework or in an init method) but TypeScript cannot see the assignment. The definite assignment assertion (!) tells the compiler "trust me, this will be set": name!: string. Use it sparingly — it bypasses the compiler's check. Prefer initializing in the constructor or using a setter that the compiler can track.

Example

              // With strictNullChecks, null must be explicit.
let username: string | null = null;
username = "Alice";

// The compiler forces you to handle null before use.
function greet(name: string | null): string {
  if (name === null) {
    return "Hello, Guest";
  }
  // name is narrowed to string here.
  return "Hello, " + name.toUpperCase();
}

// Definite assignment assertion — initialized later.
class Service {
  private client!: string; // Will be set by init()

  init(): void {
    this.client = "connected";
  }

  status(): string {
    return this.client;
  }
}

console.log(greet(null));
console.log(greet("bob"));
const svc = new Service();
svc.init();
console.log(svc.status());
            

strictNullChecks makes null and undefined explicit — they cannot sneak into other types. Use T | null to model absence. The definite assignment assertion (!) handles late initialization but should be used sparingly.

Try it

  • JSON Validator

    Validate JSON for null and undefined fields before processing.

  • JSON to TypeScript

    Generated types include null unions for nullable JSON fields.

Common mistakes

The mistake

Disabling strictNullChecks to avoid null-handling errors

The fix

Keep strictNullChecks on. Handle null explicitly with a check, a default value, or optional chaining. Disabling it reintroduces runtime null errors.

The mistake

Using the non-null assertion (!) everywhere instead of handling null

The fix

The ! operator suppresses the check without verifying. Use it only when you have proof the value is non-null. Otherwise, handle the null case with a conditional or default.

Practice Null and Undefined in TypeScript

1 exercise

Practice Null and Undefined in TypeScript

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

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

View errors