0
0
Typescriptprogramming~20 mins

Unknown type vs any type in Typescript - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Unknown vs Any Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output difference between unknown and any types
What will be the output of the following TypeScript code?
Typescript
let a: unknown = 5;
let b: any = 5;

// Trying to add 10 to both variables
// console.log(a + 10); // Uncommenting this line causes error
console.log(b + 10);
A15
BCompilation error on line with 'a + 10'
CTypeError at runtime
DNaN
Attempts:
2 left
💡 Hint
Consider how TypeScript treats operations on unknown vs any types.
🧠 Conceptual
intermediate
1:30remaining
Why use unknown instead of any?
Which statement best explains why you might prefer unknown over any in TypeScript?
A<code>unknown</code> forces you to check the type before using the value, improving safety.
B<code>unknown</code> disables all type checking, making coding faster.
C<code>any</code> is safer because it prevents runtime errors.
D<code>any</code> requires explicit type assertions before use.
Attempts:
2 left
💡 Hint
Think about how each type affects type safety and error prevention.
🔧 Debug
advanced
2:00remaining
Identify the error with unknown type usage
What error will this TypeScript code produce?
Typescript
function process(value: unknown) {
  console.log(value.toFixed(2));
}
process(3.1415);
ACompilation error: Object is of type 'unknown'.
BOutputs '3.14' without error.
CRuntime error: toFixed is not a function.
DCompilation error: Missing return type.
Attempts:
2 left
💡 Hint
Consider what TypeScript allows you to do with unknown types without type assertions.
Predict Output
advanced
2:00remaining
Result of type assertion from unknown to number
What will be the output of this TypeScript code?
Typescript
let val: unknown = "123";
let num = val as number;
console.log(num + 1);
A124
BRuntime output: NaN
CRuntime output: '1231'
DCompilation error: Cannot assert string to number.
Attempts:
2 left
💡 Hint
Think about what happens when you assert a string as a number and then add 1.
🧠 Conceptual
expert
2:30remaining
Difference in type safety between unknown and any
Which statement correctly describes the difference in type safety between unknown and any in TypeScript?
A<code>any</code> requires type assertions, but <code>unknown</code> does not.
B<code>any</code> is safer because it allows flexible usage without errors, while <code>unknown</code> causes runtime errors.
C<code>unknown</code> and <code>any</code> behave identically in all cases.
D<code>unknown</code> is safer because it requires explicit type checks before usage, while <code>any</code> bypasses all checks.
Attempts:
2 left
💡 Hint
Focus on how each type affects compile-time safety and runtime behavior.