What is the difference between unknown and {} and object in TypeScript?
Why Interviewers Ask This
This is a differentiating question used for senior and lead roles. Interviewers want to see if you can explain not just what happens, but why — and what the trade-offs are in different approaches.
Answer
These three types are commonly confused. unknown: the safest — represents any possible value including null, undefined, numbers, strings, objects. You cannot access any properties on unknown without type narrowing. {} (empty object type): represents any non-nullish value — accepts strings, numbers, booleans, functions, objects, but NOT null or undefined (in strict mode). Despite the syntax, it does NOT mean "an empty object" — it means "any value that is not null or undefined." TypeScript allows calling any property on {} values — but those accesses are untyped. object: represents any non-primitive value — objects, functions, arrays, but NOT string, number, boolean, symbol, or bigint. Useful when you want to accept object references but not primitive values. Usage guidance: use unknown when the type is truly unknown and you want to force narrowing. Avoid {} — it is surprisingly broad. Use object when you want to accept any object-like value. For typed objects, use specific interfaces or Record<string, unknown>.
Pro Tip
Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a TypeScript codebase.
Previous
How does TypeScript handle circular references in types?
Next
What are const type parameters in TypeScript 5.0?
More TypeScript Questions
View all →- Advanced What is structural typing in TypeScript?
- Advanced What are branded types (opaque types) in TypeScript?
- Advanced What is the Variance annotation in TypeScript 4.7?
- Advanced What is the satisfies operator used for in TypeScript?
- Advanced How do you implement a deep partial type in TypeScript?