0Pricing
TypeScript Academy · Lesson

Boolean Literals and Literal Inference

Understand how TypeScript widens or narrows literal inference.

Boolean Literal Types

The type boolean is really the union true | false. Each of those is a boolean literal type: a type that allows only one specific boolean value.

let yes: true = true;
let no: false = false;
console.log(yes, no);
// yes = false; // Error: false not assignable to true

Boolean Literals in Unions

Boolean literals shine when paired with other literals to model discriminated outcomes, such as a success flag combined with a payload shape.

type Result =
  | { ok: true; value: number }
  | { ok: false; error: string };

const r: Result = { ok: true, value: 42 };
console.log(r);

All lessons in this course

  1. String and Numeric Literal Types
  2. Boolean Literals and Literal Inference
  3. const Assertions with as const
  4. Combining Literals into Unions
← Back to TypeScript Academy