Boolean as Literal Types
What you'll learn: The values true and false aren't just boolean values—they're also their own distinct literal types in TypeScript.
The Core Idea
You already know about boolean as a type. But here's the twist: true and false are themselves types, just like the string literal "hello" or the number literal 42.
When you write true, TypeScript doesn't just see "a boolean value." It sees the specific literal type true. Same with false—it's its own type.
Think of It This Way
The type boolean is actually a union of two literal types: true | false. It's like saying "number" encompasses all possible numbers, while 5 is one specific number literal type. Similarly, boolean encompasses both boolean literal types.
Why This Matters
This means you can use true and false as precise type constraints:
- A function can accept only
true, notfalse - A property can be locked to only
false, nevertrue - You can create unions that include one boolean literal but not the other
This gives you fine-grained control when modeling data that has specific boolean requirements, not just "any true/false value."
The Practical Impact
Understanding that true and false are literal types helps you recognize why const flag = true gives you type true (literal), while let flag = true gives you type boolean (widened from the literal). The literal types are the building blocks; boolean is their combination.
Key Takeaway: true and false are distinct literal types in TypeScript, just like "hello" or 42. The type boolean is simply the union true | false.