TypeScript · Junior · Question 3 of 75
How does unknown differ from any?
- ✓Correct answer: You can't do anything with
unknownwithout narrowing, whileanydisables checks - 2
unknowncan be assigned to any variable, whileanyonly toobject - 3They're synonyms;
unknownis just a newer name forany
Explanation
any turns typing off and "infects" everything it touches. unknown is the safe counterpart: the value can be anything, but you can use it only after a check (typeof x === 'string'). For data from JSON.parse and catch (e), unknown is the right choice.