Bird
Raised Fist0
Typescriptprogramming~20 mins

Unknown type vs any type in Typescript - Practice Questions

Choose your learning style10 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.

Practice

(1/5)
1. What is the main difference between unknown and any types in TypeScript?
easy
A. unknown requires type checking before use, any does not.
B. any requires type checking before use, unknown does not.
C. unknown and any behave exactly the same.
D. any is safer than unknown.

Solution

  1. Step 1: Understand unknown behavior

    unknown means the value can be anything, but you must check its type before using it to avoid errors.
  2. Step 2: Understand any behavior

    any disables type checking, so you can use the value without checks, which can cause runtime errors.
  3. Final Answer:

    unknown requires type checking before use, any does not. -> Option A
  4. Quick Check:

    unknown = safe, any = unchecked [OK]
Hint: Remember: unknown forces checks, any skips them [OK]
Common Mistakes:
  • Thinking any is safer than unknown
  • Assuming unknown allows direct use without checks
  • Believing unknown and any behave the same
2. Which of the following is the correct way to assign a value of type unknown to a variable of type string?
easy
A. let a: unknown = "hello"; let b: string = a as string;
B. let a: unknown = "hello"; let b: string = a.toString();
C. let a: unknown = "hello"; let b: string = a + '';
D. let a: unknown = "hello"; let b: string = a;

Solution

  1. Step 1: Understand assignment rules for unknown

    You cannot assign unknown directly to a more specific type without a type assertion or check.
  2. Step 2: Check options for correct syntax

    let a: unknown = "hello"; let b: string = a as string; uses a type assertion as string, which is the correct way to tell TypeScript the value is a string.
  3. Final Answer:

    let b: string = a as string; -> Option A
  4. Quick Check:

    Use as to convert unknown safely [OK]
Hint: Use 'as' to convert unknown to a specific type [OK]
Common Mistakes:
  • Assigning unknown directly without assertion
  • Using incorrect type assertion syntax
  • Calling methods on unknown without checks
3. What will be the output of the following TypeScript code?
let value: unknown = 42;
if (typeof value === 'number') {
  console.log(value + 8);
} else {
  console.log('Not a number');
}
medium
A. TypeError at runtime
B. Not a number
C. 50
D. Compilation error

Solution

  1. Step 1: Check the type of value

    The variable value is unknown but assigned 42, a number.
  2. Step 2: Evaluate the if condition and output

    The condition typeof value === 'number' is true, so it logs value + 8 which is 42 + 8 = 50.
  3. Final Answer:

    50 -> Option C
  4. Quick Check:

    Type check passes, output is 50 [OK]
Hint: Check type before using unknown to avoid errors [OK]
Common Mistakes:
  • Assuming unknown can be used without type check
  • Expecting runtime error without type check
  • Confusing unknown with any behavior
4. Identify the error in this TypeScript code snippet:
let data: unknown = 'hello';
let length: number = data.length;
medium
A. No error, code runs fine.
B. Cannot access property 'length' on type 'unknown'.
C. Type 'string' is not assignable to type 'unknown'.
D. Variable 'length' should be type 'string'.

Solution

  1. Step 1: Understand property access on unknown

    You cannot access properties directly on a variable typed as unknown without type checking or assertion.
  2. Step 2: Identify the error message

    Trying to access data.length causes a compile-time error: "Cannot access property 'length' on type 'unknown'."
  3. Final Answer:

    Cannot access property 'length' on type 'unknown'. -> Option B
  4. Quick Check:

    Accessing unknown properties causes error [OK]
Hint: Check type before accessing unknown properties [OK]
Common Mistakes:
  • Assuming unknown behaves like any
  • Ignoring compiler error on property access
  • Wrongly typing variables
5. You receive data from an external API typed as any. You want to safely use it as a string only if it really is a string. Which approach is best?
hard
A. Assign the any data directly to a string variable.
B. Cast the any data to unknown and then to string without checks.
C. Ignore types and use the data as is.
D. Use a type guard to check if data is a string before using it.

Solution

  1. Step 1: Understand risks of any

    any disables type checking, so using it directly can cause runtime errors if the data is not as expected.
  2. Step 2: Use type guard for safety

    Checking the type with a type guard (e.g., typeof data === 'string') ensures safe usage only when the data is a string.
  3. Final Answer:

    Use a type guard to check if data is a string before using it. -> Option D
  4. Quick Check:

    Type guard ensures safe use of any data [OK]
Hint: Always check any data type before use [OK]
Common Mistakes:
  • Assigning any directly without checks
  • Casting any to unknown then string without validation
  • Ignoring type safety for external data
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard