Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Literal and Object Types

TypeScript boolean literal types (true and false)

Reference for the true and false literal types in TypeScript: how boolean is a union, discriminating on flags, conditional returns and narrowing by truthiness.

true and false are types as well as values. Each contains exactly one value, and the familiar boolean type is simply the union true | false. Knowing this explains several behaviors: narrowing a boolean with a check leaves you with one of the two literals, and a function that always returns true can say so in its type.

Boolean literal types are most useful as tags. A property typed ok: true on one object and ok: false on another makes a discriminated union, which is the foundation of result types and loading states.

TypeScript
type Flag = true;
type Either = true | false;   // identical to boolean

Try it

TypeScript Discriminating on a boolean

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript boolean is true | false

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

TypeScript
type A = true;
type B = false;
type C = boolean;            // true | false
interface Flags { enabled: true }
Form Description
true The type with the single value true.
false The type with the single value false.
boolean Shorthand for the union of the two.
!x, x === true Expressions whose result is boolean, or a literal when the operand types allow it.

Edge cases: the type boolean is a closed union, so a switch over it with cases for true and false is exhaustive. The wrapper type Boolean (capital B) is an object type and should not be used for annotations.

Usage notes

  • Narrowing works because boolean expands to true | false. After if (flag), the variable is true inside and false after a returning branch.
  • A boolean tag only works as a discriminant when each member uses a literal: ok: boolean on both members would not narrow anything.
  • const done = false has type false, while let done = false widens to boolean. Object literals also widen their boolean properties unless you use as const.
  • Function overloads with literal boolean parameters can give precise return types, as open does above. For many flags, an options object with a union is usually clearer than several booleans.
  • Truthy and Falsy are not types. TypeScript narrows on truthiness checks, removing null, undefined, 0, "" and false from a union, but there is no type for "any truthy value".
  • Conditional types often return true or false to answer type-level questions, such as IsString<T> above.
  • x is true can be a return type for a type predicate when you want to narrow from boolean | undefined.
  • Avoid == true comparisons; they add noise and, for non-boolean operands, coercion.