Course contentsShow
← TypeScript
Lesson 200 of 1,7965. Type Narrowing FundamentalsFree lesson

Falsy Value Types

Understanding how 0, empty string, false, null, and undefined affect type narrowing in conditionals.

Falsy Value Types

You'll learn: how TypeScript handles the six falsy values (0, "", false, null, undefined, and NaN) when narrowing types in conditional checks.

Understanding Falsy Values in Type Narrowing

JavaScript has exactly six falsy values — values that evaluate to false in boolean contexts. TypeScript needs to understand these when narrowing types because a truthiness check doesn't just eliminate null and undefined; it also excludes other falsy values.

When you check if (value), TypeScript narrows out all falsy types from unions:

function process(input: string | number | null | undefined) {
  if (input) {
    // input is narrowed to: string | number
    // BUT: empty string "" and 0 are also excluded!
  }
}

This is crucial: truthiness narrowing removes legitimate values like 0 or "", not just null/undefined.

The Falsy Type Landscape

Each falsy value has its own type signature:

  • 0 has type number (but is falsy)
  • "" has type string (but is falsy)
  • false has type boolean
  • null has type null
  • undefined has type undefined
  • NaN has type number (but is falsy)

When you use truthiness narrowing, TypeScript doesn't distinguish which kind of falsy value was eliminated — it removes them all. This means if (count) will incorrectly skip processing when count is 0, even though zero is a valid number.

Key Takeaway: Truthiness narrowing removes all falsy types from unions, including valid values like 0 and "". Use explicit checks (!= null, !== undefined) when falsy values are meaningful data.