What if your code could instantly know exactly what kind of data it's handling, every time?
Why Custom type guard functions in Typescript? - Purpose & Use Cases
Imagine you have a box full of different toys, but you want to pick only the cars. Without a clear way to check, you have to guess or check each toy manually every time.
Manually checking each item's type is slow and error-prone. You might pick the wrong toy or write repeated code everywhere, making your program messy and hard to fix.
Custom type guard functions act like a smart filter that tells you exactly if an item is a car or not. This makes your code cleaner, safer, and easier to understand.
if (item && item.type === 'car') { /* use item as car */ }
function isCar(item: any): item is Car { return item?.type === 'car'; }It enables your program to confidently and clearly separate different types, preventing mistakes and making your code smarter.
When handling user input that can be many shapes, a custom type guard helps you safely pick the right data type before using it.
Manual type checks are slow and risky.
Custom type guards give clear, reusable checks.
They make your code safer and easier to maintain.
Practice
Solution
Step 1: Understand what a type guard does
A type guard checks if a value is of a certain type at runtime.Step 2: Recognize the benefit in TypeScript
It helps TypeScript narrow the type, making code safer and clearer.Final Answer:
To check if a value matches a specific type and help TypeScript narrow types -> Option CQuick Check:
Type guard = check type + narrow [OK]
- Thinking type guards convert types
- Confusing type guards with error handling
- Believing type guards declare new types
isString that checks if a value is a string?Solution
Step 1: Identify the return type for a type guard
A custom type guard returns a type predicate likevalue is string.Step 2: Check the function body
The function should returntrueiftypeof value === 'string'.Final Answer:
function isString(value: any): value is string { return typeof value === 'string'; } -> Option AQuick Check:
Type guard syntax = value is Type [OK]
- Using boolean instead of type predicate as return type
- Returning wrong type predicate
- Returning the type instead of a boolean
function isNumber(value: any): value is number {
return typeof value === 'number';
}
const input: any = 42;
if (isNumber(input)) {
console.log(input.toFixed(2));
} else {
console.log('Not a number');
}Solution
Step 1: Check the type guard function behavior
The functionisNumberreturns true ifvalueis a number.Step 2: Analyze the if condition and output
Sinceinputis 42 (a number), the if block runs andinput.toFixed(2)outputs '42.00'.Final Answer:
42.00 -> Option AQuick Check:
isNumber(42) = true, so prints 42.00 [OK]
- Assuming input is not a number
- Expecting runtime error on toFixed
- Confusing output with string conversion
function isArray(value: any): value is Array {
return Array.isArray(value);
}Solution
Step 1: Understand the return type syntax
The return type should be a type predicate with a specific type, e.g.,value is any[]for arrays.Step 2: Identify the incorrect type usage
Arrayalone is a constructor, not a type for arrays. Useany[]or a typed array instead.Final Answer:
Change return type to 'value is any[]' instead of 'value is Array' -> Option BQuick Check:
Array type predicate = value is any[] [OK]
- Using 'Array' instead of 'any[]' or typed array
- Changing function name unnecessarily
- Using typeof 'array' which is invalid
Dog using a custom type guard?interface Dog { bark(): void; }
interface Cat { meow(): void; }
type Pet = Dog | Cat;
function isDog(pet: Pet): pet is Dog {
// Which implementation is correct?
}Solution
Step 1: Understand the interfaces and type guard goal
We want to check ifpethas abarkmethod to confirm it's aDog.Step 2: Evaluate the implementations
Directly accessingpet.barkorpet.meowcauses TypeScript compile errors because those properties do not exist on the union typePet.pet instanceof Dogdoes not work becauseDogis an interface, not a class with a constructor.
Using(pet as Dog).bark !== undefinedcompiles due to the type assertion and performs a runtime check to see if thebarkproperty exists.Final Answer:
return (pet as Dog).bark !== undefined; -> Option DQuick Check:
Type assertion enables property check in union type guards [OK]
- Using instanceof with interfaces
- Directly accessing properties on union types
- Checking absence of other type's method
Ready to prove it?
Pass the test and this pattern moves unknown
