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.
type Flag = true; type Either = true | false; // identical to boolean
Try it
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.
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
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
booleanexpands totrue | false. Afterif (flag), the variable istrueinside andfalseafter a returning branch. - A boolean tag only works as a discriminant when each member uses a literal:
ok: booleanon both members would not narrow anything. const done = falsehas typefalse, whilelet done = falsewidens toboolean. Object literals also widen their boolean properties unless you useas const.- Function overloads with literal boolean parameters can give precise return types, as
opendoes above. For many flags, an options object with a union is usually clearer than several booleans. TruthyandFalsyare not types. TypeScript narrows on truthiness checks, removingnull,undefined,0,""andfalsefrom a union, but there is no type for "any truthy value".- Conditional types often return
trueorfalseto answer type-level questions, such asIsString<T>above. x is truecan be a return type for a type predicate when you want to narrow fromboolean | undefined.- Avoid
== truecomparisons; they add noise and, for non-boolean operands, coercion.