Challenge - 5 Problems
Custom Type Guard Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a custom type guard function
What is the output of the following TypeScript code when run with
console.log(isStringOrNumber(42));?Typescript
function isStringOrNumber(value: unknown): value is string | number { return typeof value === 'string' || typeof value === 'number'; } console.log(isStringOrNumber(42));
Attempts:
2 left
💡 Hint
Think about what the function checks for the input value.
✗ Incorrect
The function checks if the input is a string or a number. Since 42 is a number, it returns true.
🧠 Conceptual
intermediate1:30remaining
Purpose of custom type guard functions
What is the main purpose of a custom type guard function in TypeScript?
Attempts:
2 left
💡 Hint
Think about how TypeScript uses these functions to help with type safety.
✗ Incorrect
Custom type guard functions check types at runtime and tell the compiler about the type, enabling safer code.
🔧 Debug
advanced2:00remaining
Identify the error in this custom type guard
What error will this TypeScript code produce when compiled?
function isArrayOfStrings(value: any): value is string[] {
return Array.isArray(value) && value.every(item => typeof item === 'string');
}
const test = isArrayOfStrings(['hello', 5]);Typescript
function isArrayOfStrings(value: any): value is string[] { return Array.isArray(value) && value.every(item => typeof item === 'string'); } const test = isArrayOfStrings(['hello', 5]);
Attempts:
2 left
💡 Hint
Check what the function returns for the given input.
✗ Incorrect
The function returns false because not all items are strings. No compile or runtime error occurs.
📝 Syntax
advanced1:30remaining
Correct syntax for a custom type guard
Which option shows the correct syntax for a custom type guard function that checks if a value is a boolean?
Attempts:
2 left
💡 Hint
Remember the exact syntax for type predicates in TypeScript.
✗ Incorrect
Option B uses the correct syntax:
value is boolean as the return type.🚀 Application
expert2:30remaining
Using a custom type guard in conditional code
Given the custom type guard below, what will be the output of the code?
interface Cat { meow: () => void; }
interface Dog { bark: () => void; }
type Pet = Cat | Dog;
function isCat(pet: Pet): pet is Cat {
return (pet as Cat).meow !== undefined;
}
const pet: Pet = { bark: () => console.log('Woof!') };
if (isCat(pet)) {
pet.meow();
} else {
pet.bark();
}Typescript
interface Cat { meow: () => void; }
interface Dog { bark: () => void; }
type Pet = Cat | Dog;
function isCat(pet: Pet): pet is Cat {
return (pet as Cat).meow !== undefined;
}
const pet: Pet = { bark: () => console.log('Woof!') };
if (isCat(pet)) {
pet.meow();
} else {
pet.bark();
}Attempts:
2 left
💡 Hint
Check what the type guard returns and which branch runs.
✗ Incorrect
The pet object has only bark, so isCat returns false. The else branch runs and prints 'Woof!'.