Boolean Literals and Literal Inference
Understand how TypeScript widens or narrows literal inference.
Boolean Literals and Literal Inference is a free TypeScript Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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);Type Widening
When you initialize a variable, TypeScript decides what type to infer. Sometimes it keeps the exact literal; sometimes it widens to the general type. Understanding this is key to predictable types.
const exact = 'hello'; // type: 'hello' (literal)
let broad = 'hello'; // type: string (widened)
console.log(exact, broad);let Widens to the General Type
Because a let variable can be reassigned, TypeScript infers the general type so future assignments are allowed. A let string starts as string, not the literal.
let mood = 'happy'; // inferred as string
mood = 'sad'; // allowed
mood = 'tired'; // allowed
console.log(mood);const Keeps the Literal
A const can never be reassigned, so TypeScript infers the most specific type — the literal. This is why const greeting = 'hi' has type 'hi', not string.
const greeting = 'hi'; // type: 'hi'
type Greeting = typeof greeting; // 'hi'
const other: Greeting = 'hi';
console.log(other);Widening With Numbers and Booleans
The same rule applies to numbers and booleans. const keeps 42 or true as a literal; let widens to number or boolean.
const n = 42; // type: 42
const b = true; // type: true
let m = 42; // type: number
let c = true; // type: boolean
console.log(n, b, m, c);Why Widening Exists
Widening keeps everyday code ergonomic. If every let count = 0 were typed as the literal 0, you couldn't increment it. Widening lets reassignable variables behave naturally.
let count = 0; // widened to number
count = count + 1;
count = 10;
console.log('Count:', count);Forcing a Literal With Annotation
Sometimes you want a let to stay a literal. Add an explicit literal type annotation, and TypeScript will keep it narrow while still letting you reassign within that union.
let status: 'on' | 'off' = 'on';
status = 'off'; // allowed
console.log(status);
// status = 'paused'; // ErrorWidening in Object Properties
Object literal properties also widen by default. Even in a const object, mutable properties get the general type, because the property itself can be reassigned.
const config = { mode: 'dark' };
// config.mode has type string, not 'dark'
config.mode = 'light';
console.log(config.mode);Literal Inference With Functions
Function return values are inferred too. A function returning a const literal value still widens its return type to the general type unless you annotate it or use literal types in the signature.
function getMode(): 'dark' | 'light' {
return 'dark';
}
const m = getMode(); // type: 'dark' | 'light'
console.log(m);Choosing Literal vs General
Rule of thumb: use const or explicit literal annotations when you need exact values (config keys, discriminants). Let widening happen for ordinary mutable variables. Knowing which you'll get prevents surprising assignability errors.
const KEY = 'apiUrl'; // literal 'apiUrl'
let retries = 3; // number
let level: 'low' | 'high' = 'low'; // pinned literal
console.log(KEY, retries, level);Quick Check
Test your grasp of widening and literal inference.
Recap: Literal Inference
Key takeaways:
trueandfalseare literal types;booleanequalstrue | false.- let widens to the general type (
string,number,boolean). - const keeps the exact literal type.
- Annotate with a literal type to pin a
letto a narrow union.
Next we make immutability explicit with as const.
const literal = 'fixed'; // 'fixed'
let widened = 'fixed'; // string
let pinned: 'a' | 'b' = 'a';
console.log(literal, widened, pinned);Frequently asked questions
Is the “Boolean Literals and Literal Inference” lesson free?
Yes — the full text of “Boolean Literals and Literal Inference” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Boolean Literals and Literal Inference”?
Understand how TypeScript widens or narrows literal inference. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start TypeScript Academy?
No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Boolean Literals and Literal Inference” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this TypeScript Academy lesson?
Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- String and Numeric Literal Types
- Boolean Literals and Literal Inference
- const Assertions with as const
- Combining Literals into Unions