0
0
Typescriptprogramming~5 mins

Unknown type vs any type in Typescript - Quick Revision & Key Differences

Choose your learning style9 modes available
Recall & Review
beginner
What is the any type in TypeScript?
The any type allows a variable to hold any value without type checking. It disables TypeScript's type safety for that variable.
Click to reveal answer
beginner
What is the unknown type in TypeScript?
The unknown type represents any value but requires type checking before using it. It is safer than any because it forces you to check the type before operations.
Click to reveal answer
intermediate
How does unknown improve safety compared to any?
unknown forces you to check the type before using the value, preventing errors. any skips checks and can cause runtime errors.
Click to reveal answer
intermediate
Can you assign a value of type any to a variable of type unknown?
Yes, you can assign any to unknown because unknown accepts any value safely.
Click to reveal answer
intermediate
Can you assign a value of type unknown to a variable of type any?
Yes, unknown can be assigned to any because any accepts all types without checks.
Click to reveal answer
Which type requires you to check the type before using the value?
Aany
Bstring
Cunknown
Dnumber
What happens if you use any type in TypeScript?
AYou can assign any value and use it without checks
BTypeScript enforces strict type checks
CYou must check the type before use
DIt only accepts string values
Can you assign a value of type unknown directly to a variable of type string?
ANo, you must check or cast first
BYes, always
COnly if the value is a string literal
DOnly in JavaScript, not TypeScript
Which type is safer to use when you don't know the exact type yet?
Aany
Bvoid
Cnever
Dunknown
What is a key difference between any and unknown?
A<code>any</code> requires type checks, <code>unknown</code> does not
B<code>unknown</code> requires type checks, <code>any</code> does not
CBoth behave exactly the same
D<code>any</code> is safer than <code>unknown</code>
Explain the difference between any and unknown types in TypeScript.
Think about safety and when you can use the value without checks.
You got /4 concepts.
    When should you prefer unknown over any in your TypeScript code?
    Consider safety and error prevention.
    You got /4 concepts.