What is the unknown type and how does it differ from any?
Why Interviewers Ask This
Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid TypeScript basics — a prerequisite for any developer role.
Answer
The unknown type is the type-safe counterpart to any. Like any, a variable of type unknown can hold any value. But unlike any, you cannot perform operations on an unknown value without first narrowing its type — TypeScript forces you to verify the type before use. Example: let value: unknown = getData();. Trying to call value.toUpperCase() is a compile error. You must narrow first: if (typeof value === "string") { value.toUpperCase(); }. Key differences: any disables type checking entirely (unsafe); unknown preserves safety by requiring narrowing. Use unknown for: function parameters that accept any type (API responses, event payloads), error handling (catch(error: unknown)), and any situation where the type is truly not known at write time but must be verified before use. Always prefer unknown over any.
Pro Tip
Before answering, structure your response: one-line definition → real-world analogy → concrete example from a project. This makes even complex TypeScript answers easy to follow.