0
0
Typescriptprogramming~20 mins

Runtime type checking strategies in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Runtime Type Checking Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple runtime type guard function
What is the output of this TypeScript code when calling checkType(42)?
Typescript
function isNumber(value: unknown): value is number {
  return typeof value === 'number';
}

function checkType(value: unknown) {
  if (isNumber(value)) {
    return `Number: ${value}`;
  } else {
    return 'Not a number';
  }
}

console.log(checkType(42));
Aundefined
B"Not a number"
CTypeError at runtime
D"Number: 42"
Attempts:
2 left
💡 Hint
Think about what the type guard function returns for a number.
🧠 Conceptual
intermediate
1:30remaining
Understanding the purpose of runtime type checking
Why do developers use runtime type checking in TypeScript even though TypeScript provides static type checking?
ATo catch type errors during program execution that static checking cannot detect
BTo improve compile time performance
CTo replace static type checking completely
DTo avoid writing type annotations in code
Attempts:
2 left
💡 Hint
Think about what static type checking cannot verify.
Predict Output
advanced
2:00remaining
Output of a runtime type check with union types
What is the output of this TypeScript code snippet?
Typescript
type Shape = { kind: 'circle'; radius: number } | { kind: 'square'; side: number };

function area(shape: Shape): number {
  switch (shape.kind) {
    case 'circle':
      return Math.PI * shape.radius ** 2;
    case 'square':
      return shape.side ** 2;
  }
}

console.log(area({ kind: 'circle', radius: 3 }));
A28.274333882308138
B9
Cundefined
DTypeError at runtime
Attempts:
2 left
💡 Hint
Recall the formula for the area of a circle.
🔧 Debug
advanced
2:00remaining
Identify the runtime error in type checking code
What error does this code produce when run, and why?
Typescript
function isStringArray(value: unknown): value is string[] {
  return Array.isArray(value) && value.every(item => typeof item === 'string');
}

const data: unknown = ['hello', 42];

if (isStringArray(data)) {
  console.log(data.join(', '));
} else {
  console.log('Not a string array');
}
ASyntaxError due to missing colon
BThrows TypeError because join is called on a non-array
CLogs 'Not a string array' because 42 is not a string
DLogs 'hello, 42' incorrectly
Attempts:
2 left
💡 Hint
Check what the type guard returns for the array with mixed types.
🚀 Application
expert
2:30remaining
Determine the number of valid keys after runtime filtering
Given this runtime type check filtering an object, how many keys remain in the filtered object?
Typescript
function filterStringValues(obj: Record<string, unknown>): Record<string, string> {
  const result: Record<string, string> = {};
  for (const key in obj) {
    if (typeof obj[key] === 'string') {
      result[key] = obj[key] as string;
    }
  }
  return result;
}

const input = { a: 'apple', b: 42, c: 'cat', d: true, e: 'elephant' };
const filtered = filterStringValues(input);
console.log(Object.keys(filtered).length);
A5
B3
C4
D2
Attempts:
2 left
💡 Hint
Count how many values in the input object are strings.