TypeScript·Intermediate·Coding·1 min read
How do unknown and any differ?
Short interview answer
unknown accepts any input but requires narrowing before use, preserving type safety at untrusted boundaries. any disables checking and spreads unsafety through expressions. Parse external data as unknown and validate it.
Example
const a: any = JSON.parse(s);a.foo.bar; // no error — unsafe
const u: unknown = JSON.parse(s);u.foo; // Error — must narrow firstKey takeaway
Explain the underlying mental model clearly, then support it with a concrete example and its trade-offs.