Challenge - 5 Problems
Type Predicate Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of type predicate function usage
What is the output of this TypeScript code when run with
console.log(filterStrings([1, 'hello', true, 'world']))?Typescript
function isString(value: unknown): value is string { return typeof value === 'string'; } function filterStrings(arr: unknown[]): string[] { return arr.filter(isString); } console.log(filterStrings([1, 'hello', true, 'world']));
Attempts:
2 left
💡 Hint
Think about what the type predicate function
isString returns for each element.✗ Incorrect
The
isString function checks if a value is a string. The filterStrings function uses it to keep only strings from the array. So the output is an array of strings: ["hello", "world"].🧠 Conceptual
intermediate1:30remaining
Understanding type predicate return type
Which of the following correctly declares a type predicate function that checks if a value is a number?
Attempts:
2 left
💡 Hint
Type predicates use the syntax
paramName is Type as the return type.✗ Incorrect
Option B correctly uses
value is number as the return type, which tells TypeScript this function narrows the type to number. Option B returns boolean but does not narrow type. Option B uses Number (the object type) instead of number (the primitive). Option B returns a union type which is incorrect for a type predicate.🔧 Debug
advanced2:00remaining
Why does this type predicate not narrow correctly?
Consider this code snippet:
function isArrayOfStrings(value: unknown): value is string[] {
return Array.isArray(value) && value.every(elem => typeof elem === 'string');
}
const data: unknown = ['a', 'b', 3];
if (isArrayOfStrings(data)) {
console.log(data.map(s => s.toUpperCase()));
} else {
console.log('Not an array of strings');
}
What will happen when this code runs?Attempts:
2 left
💡 Hint
Check what
value.every returns when one element is not a string.✗ Incorrect
The function
isArrayOfStrings returns false because value.every(elem => typeof elem === 'string') is false due to the number 3. So the else branch runs and prints 'Not an array of strings'.📝 Syntax
advanced1:30remaining
Identify the syntax error in this type predicate function
Which option contains a syntax error in the type predicate declaration?
Typescript
function isBoolean(value: unknown): value is boolean { return typeof value === 'boolean'; }
Attempts:
2 left
💡 Hint
Check the function parameter syntax carefully.
✗ Incorrect
Option C is missing a colon between parameter name and type (
value unknown instead of value: unknown), causing a syntax error. Option C uses 'bool' which is a logic error but not syntax error. Option C is missing a semicolon but that's allowed in TypeScript. Option C is correct.🚀 Application
expert2:30remaining
Using type predicates to narrow union types in complex objects
Given these types and function, what is the output of
processItem({ type: 'circle', radius: 5 })?
type Circle = { type: 'circle'; radius: number };
type Square = { type: 'square'; side: number };
type Shape = Circle | Square;
function isCircle(shape: Shape): shape is Circle {
return shape.type === 'circle';
}
function processItem(item: Shape) {
if (isCircle(item)) {
return `Circle area: ${Math.PI * item.radius ** 2}`;
} else {
return `Square area: ${item.side ** 2}`;
}
}
console.log(processItem({ type: 'circle', radius: 5 }));Attempts:
2 left
💡 Hint
Check how the type predicate narrows the union type to access radius safely.
✗ Incorrect
The
isCircle function narrows the type to Circle when shape.type === 'circle'. So inside the if block, item.radius is valid. The area is π * 5² = 78.53981633974483.