typeof and Truthiness Narrowing
Use typeof checks to narrow primitive types.
typeof and Truthiness Narrowing is a free TypeScript Academy lesson on CoddyKit — lesson 1 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.
Welcome
typeof Narrowing
function process(val: string | number) {
if (typeof val === 'string') {
return val.toUpperCase(); // val: string
}
return val.toFixed(2); // val: number
}typeof Returns
typeof 'hello' // 'string'
typeof 42 // 'number'
typeof true // 'boolean'
typeof null // 'object' (quirk!)
typeof undefined // 'undefined'Truthiness Narrowing
function greet(name: string | null) {
if (name) {
console.log('Hello, ' + name); // name: string
} else {
console.log('Hello, stranger');
}
}Falsy Values
Narrowing with Boolean Coercion
const items: (string | null)[] = ['a', null, 'b', null];
const strings = items.filter((x): x is string => x !== null);Negated Conditions
function getLength(val: string | null): number {
if (!val) return 0; // early return narrows
return val.length; // val: string here
}Combining typeof with Equality
function accept(x: string | number | boolean) {
if (typeof x === 'string' || typeof x === 'number') {
console.log(x); // x: string | number
}
}typeof Cannot Narrow to null
function check(x: string | null | 0) {
if (x === null) { /* x: null */ }
else if (x === 0) { /* x: 0 */ }
else { /* x: string */ }
}Narrowing in Loops
Control Flow Analysis
let x: string | number = 'hello';
// x: string
x = 42;
// x: numberQuick Check
Recap
Frequently asked questions
Is the “typeof and Truthiness Narrowing” lesson free?
Yes — the full text of “typeof and Truthiness Narrowing” 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 “typeof and Truthiness Narrowing”?
Use typeof checks to narrow primitive types. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “typeof and Truthiness Narrowing” 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
- typeof and Truthiness Narrowing
- instanceof and in Narrowing
- User-Defined Type Guard Functions
- Exhaustiveness Checking with never