Unknown vs Any
Question
How does unknown differ from any?
Hint
unknown is the type-safe alternative to any.
Explanation
Both any and unknown accept any value. The difference: any disables ALL type checking — you can call any method, access any property without error. unknown requires type narrowing before use — you must check the type (typeof, instanceof, type guard) before operating on the value. unknown is the type-safe alternative to any. Prefer unknown for values of unknown type (e.g., JSON.parse returns any, but should be treated as unknown). any should be avoided except for gradual migration or truly dynamic code.
Related Resources
Related Lessons
- Basic Types
Learn basic types.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.