Challenge - 5 Problems
Runtime Type Checking Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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));
Attempts:
2 left
💡 Hint
Think about what the type guard function returns for a number.
✗ Incorrect
The function
isNumber checks if the value's type is 'number'. Since 42 is a number, the condition is true, so the function returns the string 'Number: 42'.🧠 Conceptual
intermediate1:30remaining
Understanding the purpose of runtime type checking
Why do developers use runtime type checking in TypeScript even though TypeScript provides static type checking?
Attempts:
2 left
💡 Hint
Think about what static type checking cannot verify.
✗ Incorrect
Static type checking happens before running the program and cannot verify types of data coming from external sources like user input or APIs. Runtime type checking helps catch these errors during execution.
❓ Predict Output
advanced2: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 }));Attempts:
2 left
💡 Hint
Recall the formula for the area of a circle.
✗ Incorrect
The shape is a circle with radius 3, so the area is π * 3^2 = 28.274333882308138 approximately.
🔧 Debug
advanced2: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'); }
Attempts:
2 left
💡 Hint
Check what the type guard returns for the array with mixed types.
✗ Incorrect
The type guard returns false because not all items are strings. So the else branch runs and logs 'Not a string array'.
🚀 Application
expert2: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);
Attempts:
2 left
💡 Hint
Count how many values in the input object are strings.
✗ Incorrect
Only keys 'a', 'c', and 'e' have string values. So the filtered object has 3 keys.