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 trueBoolean 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
- String and Numeric Literal Types
- Boolean Literals and Literal Inference
- const Assertions with as const
- Combining Literals into Unions