Advanced Type Narrowing
Learn how to use advanced type guards and control flow analysis to handle complex type conditions with ease.
Advanced Type Narrowing is a free TypeScript Academy lesson on CoddyKit — lesson 1 of 3. 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 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Introduction to Advanced Type Narrowing
Advanced type narrowing allows you to refine complex types at runtime using advanced techniques like type guards, control flow analysis, and custom predicates.
These tools make your TypeScript code safer and more predictable.

2
What is Type Narrowing?
Type narrowing is the process of refining a broader type (e.g., string | number) into a more specific type using runtime checks.
3
Using typeof for Type Narrowing
The typeof operator can be used to narrow down primitive types:
function printValue(value: string | number): void {
if (typeof value === 'string') {
console.log(`String: ${value}`);
} else {
console.log(`Number: ${value}`);
}
}
printValue('Hello'); // String: Hello
printValue(42); // Number: 424
Using instanceof for Type Narrowing
The instanceof operator can be used to narrow down class instances:
class Dog {
bark() {
console.log('Woof!');
}
}
class Cat {
meow() {
console.log('Meow!');
}
}
function makeSound(animal: Dog | Cat): void {
if (animal instanceof Dog) {
animal.bark();
} else {
animal.meow();
}
}
makeSound(new Dog()); // Woof!
makeSound(new Cat()); // Meow!5
Using Custom Type Guards
You can create custom type guard functions to narrow types more precisely:
type Fish = { swim: () => void };
type Bird = { fly: () => void };
declare const animal: Fish | Bird;
function isFish(animal: Fish | Bird): animal is Fish {
return (animal as Fish).swim !== undefined;
}
if (isFish(animal)) {
animal.swim();
} else {
animal.fly();6
Control Flow Analysis
TypeScript’s control flow analysis automatically narrows types based on the logical flow of your code:
function getLength(value: string | string[]): number {
if (Array.isArray(value)) {
return value.length;
} else {
return value.length;
}
}
console.log(getLength('Hello')); // 5
console.log(getLength(['a', 'b', 'c'])); // 37
Benefits of Advanced Type Narrowing
Advanced type narrowing improves your code by:
- Reducing runtime errors.
- Making code safer and easier to understand.
- Providing precise control over type handling.
8
9
Practice Task
Create a function that takes a parameter of type string | number and logs a different message for each type. Use typeof to implement the logic.
10
Congratulations!
You’ve learned how to use advanced type narrowing techniques in TypeScript to refine and handle complex types safely and effectively. Practice these techniques to make your code more robust and type-safe!

Frequently asked questions
Is the “Advanced Type Narrowing” lesson free?
Yes — the full text of “Advanced Type Narrowing” is free to read here on the web, and the TypeScript Academy course includes 3 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 “Advanced Type Narrowing”?
Learn how to use advanced type guards and control flow analysis to handle complex type conditions with ease. 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 3, so you can start here or from the beginning and move at your own pace.
How long does the “Advanced Type 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
- Advanced Type Narrowing
- Advanced Class Features
- Conditional Types