0
0
Typescriptprogramming~20 mins

Type predicates in practice in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Type Predicate Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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']));
ATypeError at runtime
B[1, "hello", true, "world"]
C["1", "hello", "true", "world"]
D["hello", "world"]
Attempts:
2 left
💡 Hint
Think about what the type predicate function isString returns for each element.
🧠 Conceptual
intermediate
1:30remaining
Understanding type predicate return type
Which of the following correctly declares a type predicate function that checks if a value is a number?
Afunction isNumber(value: unknown): value is Number { return typeof value === 'number'; }
Bfunction isNumber(value: unknown): value is number { return typeof value === 'number'; }
Cfunction isNumber(value: unknown): boolean { return typeof value === 'number'; }
Dfunction isNumber(value: unknown): value is number | string { return typeof value === 'number'; }
Attempts:
2 left
💡 Hint
Type predicates use the syntax paramName is Type as the return type.
🔧 Debug
advanced
2: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?
AIt prints 'Not an array of strings' because data contains a number.
BIt prints ['A', 'B', '3'] converting all elements to uppercase.
CIt throws a runtime error because data is not a string array.
DIt prints ['A', 'B'] ignoring the number 3.
Attempts:
2 left
💡 Hint
Check what value.every returns when one element is not a string.
📝 Syntax
advanced
1: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';
}
Afunction isBoolean(value: unknown): value is boolean { return typeof value === 'boolean'; }
Bfunction isBoolean(value: unknown): value is boolean { return typeof value === 'boolean' }
Cfunction isBoolean(value unknown): value is boolean { return typeof value === 'boolean'; }
Dfunction isBoolean(value: unknown): value is boolean { return typeof value === 'bool'; }
Attempts:
2 left
💡 Hint
Check the function parameter syntax carefully.
🚀 Application
expert
2: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 }));
ACircle area: 78.53981633974483
BSquare area: 25
CTypeError at runtime
DCircle area: NaN
Attempts:
2 left
💡 Hint
Check how the type predicate narrows the union type to access radius safely.