0Pricing
TypeScript Academy · Lesson

typeof, equality, truthiness narrowing

Use typeof, strict equality, and truthiness to narrow union types without unsafe casts.

typeof, equality, truthiness narrowing 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.

Intro

Goal: Narrow broad union types down to exact types using typeof, strict equality, and truthiness. This unlocks safe property/method access.

typeof narrowing

typeof narrows primitives (string, number, boolean, bigint, symbol, undefined). Use it to branch safely.

function printLen(x: string | number) {
  if (typeof x === "string") {
    console.log(x.length); // x is string
  } else {
    console.log(x.toFixed(2)); // x is number
  }
}

printLen("TS");
printLen(3.14);

Strict equality

Prefer ===/!== for precise narrowing; avoid == which coerces and confuses both humans and the compiler.

function greet(x: string | null) {
  if (x !== null) {
    // x is string here
    console.log("Hello " + x.toUpperCase());
  } else {
    console.log("No name");
  }
}

greet("Ada");
greet(null);

Truthiness narrowing

Truthiness narrows too, but beware: "" and 0 are falsy even though they are valid values. Use strict checks when you must distinguish them.

function show(v: string | 0 | "" | null | undefined) {
  if (v) {
    // v is string here (non-empty)
    console.log("value:", v);
  } else {
    // v is "", 0, null, or undefined
    console.log("empty");
  }
}

show("ok");
show("");
show(0);

Combining checks

Combine checks to guard nested access. For finer control, prefer explicit u !== null && u !== undefined when 0/"" are valid cases.

type MaybeUser = { name: string } | null | undefined;

function hi(u: MaybeUser) {
  if (u && u.name) {
    console.log("Hi " + u.name);
  } else {
    console.log("No user");
  }
}

hi({ name: "Lin" });
hi(null);

Guidelines

Guidelines:

  • Use typeof for primitives.
  • Use ===/!== to avoid coercion.
  • Truthiness is concise; avoid it when empty strings/zero are meaningful.

Narrowing check

Quick check: Which check correctly narrows a value to string?

Recap

Recap: Narrow types with typeof, strict equality, and truthiness. Prefer explicit checks when 0/"" are valid values.

Frequently asked questions

Is the “typeof, equality, truthiness narrowing” lesson free?

Yes — the full text of “typeof, equality, truthiness narrowing” 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 “typeof, equality, truthiness narrowing”?

Use typeof, strict equality, and truthiness to narrow union types without unsafe casts. 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 “typeof, equality, truthiness narrowing” 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. typeof, equality, truthiness narrowing
  2. in, instanceof, discriminated unions
  3. Exhaustiveness with never (intro)
← Back to TypeScript Academy