0Pricing
TypeScript Academy · Lesson

typeof and Truthiness Narrowing

Use typeof checks to narrow primitive types.

typeof and Truthiness Narrowing is a free TypeScript Academy lesson on CoddyKit — lesson 1 of 4. 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 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome

TypeScript narrows union types inside conditional blocks. `typeof` checks and truthiness checks are the most common narrowing techniques.

typeof Narrowing

Use `typeof` to check primitive types. TypeScript understands typeof checks and narrows the type inside the block.
function process(val: string | number) {
  if (typeof val === 'string') {
    return val.toUpperCase(); // val: string
  }
  return val.toFixed(2); // val: number
}

typeof Returns

typeof returns 'string', 'number', 'boolean', 'bigint', 'symbol', 'undefined', 'object', or 'function'. Note: typeof null === 'object' — a JavaScript quirk.
typeof 'hello'    // 'string'
typeof 42         // 'number'
typeof true       // 'boolean'
typeof null       // 'object' (quirk!)
typeof undefined  // 'undefined'

Truthiness Narrowing

JavaScript values are truthy or falsy. TypeScript narrows `T | null | undefined` to `T` inside a truthiness check.
function greet(name: string | null) {
  if (name) {
    console.log('Hello, ' + name); // name: string
  } else {
    console.log('Hello, stranger');
  }
}

Falsy Values

Falsy values in JavaScript: false, 0, '', null, undefined, 0n, NaN. Truthiness narrowing removes these from the type when the check passes.

Narrowing with Boolean Coercion

Using `Boolean(val)` or `!!val` also narrows out null and undefined.
const items: (string | null)[] = ['a', null, 'b', null];
const strings = items.filter((x): x is string => x !== null);

Negated Conditions

TypeScript also narrows in else branches and after early returns.
function getLength(val: string | null): number {
  if (!val) return 0; // early return narrows
  return val.length;  // val: string here
}

Combining typeof with Equality

Strict equality (===) combined with typeof gives precise narrowing.
function accept(x: string | number | boolean) {
  if (typeof x === 'string' || typeof x === 'number') {
    console.log(x); // x: string | number
  }
}

typeof Cannot Narrow to null

To narrow null, use strict equality `=== null`. Truthiness cannot distinguish between null and other falsy values.
function check(x: string | null | 0) {
  if (x === null) { /* x: null */ }
  else if (x === 0) { /* x: 0 */ }
  else { /* x: string */ }
}

Narrowing in Loops

TypeScript applies narrowing inside loop bodies as well, not just if statements.

Control Flow Analysis

TypeScript tracks the type at each point in your code using control flow analysis. Assignments also update the type.
let x: string | number = 'hello';
// x: string
x = 42;
// x: number

Quick Check

Inside `if (typeof val === 'string')`, what is the type of `val` assuming it was `string | number`?

Recap

typeof checks and truthiness conditions narrow union types in TypeScript. TypeScript uses control flow analysis to track types at each point. Use strict equality for null/undefined checks.

Frequently asked questions

Is the “typeof and Truthiness Narrowing” lesson free?

Yes — the full text of “typeof and Truthiness Narrowing” is free to read here on the web, and the TypeScript Academy course includes 4 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 and Truthiness Narrowing”?

Use typeof checks to narrow primitive types. 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 4, so you can start here or from the beginning and move at your own pace.

How long does the “typeof and 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 and Truthiness Narrowing
  2. instanceof and in Narrowing
  3. User-Defined Type Guard Functions
  4. Exhaustiveness Checking with never
← Back to TypeScript Academy