User-Defined Type Guard Functions
Write is predicates to create custom type guards.
User-Defined Type Guard Functions is a free TypeScript Academy lesson on CoddyKit. This is lesson 3 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, and your progress syncs across the web and the CoddyKit app. The TypeScript Academy course includes 4 lessons in total.
Welcome
The is Predicate Return Type
function isString(val: unknown): val is string {
return typeof val === 'string';
}Using a Type Guard
function process(val: unknown) {
if (isString(val)) {
console.log(val.toUpperCase()); // val: string
}
}Type Guard for Interfaces
interface Cat { meow(): void }
interface Dog { bark(): void }
function isCat(pet: Cat | Dog): pet is Cat {
return 'meow' in pet;
}Type Guards for Complex Objects
interface User { name: string; age: number; }
function isUser(val: unknown): val is User {
return (
typeof val === 'object' &&
val !== null &&
typeof (val as any).name === 'string' &&
typeof (val as any).age === 'number'
);
}Type Guards vs Assertions
Array.isArray as a Type Guard
function flatten(val: string | string[]): string[] {
return Array.isArray(val) ? val : [val];
}Generic Type Guards
function isNonNull<T>(val: T | null | undefined): val is T {
return val !== null && val !== undefined;
}
const values = [1, null, 2, undefined].filter(isNonNull);
// values: number[]Assertion Functions
function assertIsString(val: unknown): asserts val is string {
if (typeof val !== 'string') throw new TypeError('Not a string');
}
assertIsString(input);
console.log(input.toUpperCase()); // narrowed to stringThe Responsibility of Type Guards
Type Guards in filter and find
const mixed: (string | null)[] = ['a', null, 'b'];
const strings = mixed.filter((x): x is string => x !== null);
// strings: string[]Quick Check
Recap
Learn TypeScript with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 101
- Lessons
- 352
Frequently Asked Questions
Is the “User-Defined Type Guard Functions” lesson free?
Yes — the full text of “User-Defined Type Guard Functions” is free to read here on the web. 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. The TypeScript Academy course includes 4 lessons in total.
What will I learn in “User-Defined Type Guard Functions”?
Write is predicates to create custom type guards. 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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
How long does the “User-Defined Type Guard Functions” 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