What is the unknown type and how does it differ from any?

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.