0Pricing
TypeScript Academy · Lesson

Strict Null Checking

Learn how to use strict null checking to eliminate null and undefined errors, ensuring safer and more predictable code.

Strict Null Checking is a free TypeScript Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Introduction to Strict Null Checking

Strict null checking is a feature in TypeScript that helps prevent null and undefined errors by enforcing type safety.

This feature ensures that you explicitly handle these cases, making your code more reliable and predictable.

Strict Null Checking — illustration 1

2

What is Strict Null Checking?

When strict null checking is enabled, TypeScript treats null and undefined as distinct types that must be handled explicitly in your code.

3

Enabling Strict Null Checking

You can enable strict null checking by adding "strictNullChecks": true in your tsconfig.json file:

{
  "compilerOptions": {
    "strictNullChecks": true
  }
}

4

Handling Null and Undefined

When strict null checking is enabled, TypeScript requires you to handle null and undefined explicitly:

function greet(name: string | null): void {
  if (name === null) {
    console.log('Hello, guest!');
  } else {
    console.log(`Hello, ${name}!`);
  }
}

greet(null);
// Output: Hello, guest!

5

Optional Chaining

Optional chaining (?.) is a feature that works well with strict null checking, allowing you to safely access properties of potentially null or undefined objects:

const user = {
  profile: {
    name: 'Alice'
  }
};

console.log(user.profile?.name); // Output: Alice
console.log(user.profile?.age); // Output: undefined

6

Non-Null Assertion Operator

The non-null assertion operator (!) tells TypeScript that a value is not null or undefined:

function getLength(text?: string): number {
  return text!.length;
}

console.log(getLength('Hello')); // Output: 5

7

Benefits of Strict Null Checking

Enabling strict null checking provides:

  • Better error prevention during development.
  • Safer and more reliable code.
  • Improved developer confidence when handling null and undefined.

8

9

Practice Task

Create a function that accepts a string or null and safely logs a message based on the input. Test it with different inputs.

10

Congratulations!

You’ve learned how to use strict null checking in TypeScript to handle null and undefined values safely. Practice using these techniques to write safer and more predictable code!

Strict Null Checking — illustration 10

Frequently asked questions

Is the “Strict Null Checking” lesson free?

Yes — the full text of “Strict Null Checking” is free to read here on the web, and the TypeScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Strict Null Checking”?

Learn how to use strict null checking to eliminate null and undefined errors, ensuring safer and more predictable code. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Strict Null Checking” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Strict Null Checking
  2. Dynamic Type Creation
  3. Module Augmentation
← Back to TypeScript Academy