TypeScript - any, unknown, never
any
Disables type checking. Use sparingly (e.g., gradual migration). Prefer narrowing unknown instead.
unknown
function parse(json: string): unknown {
return JSON.parse(json);
}
const v: unknown = parse('{"ok":true}');
if (typeof v === 'object' && v !== null && 'ok' in v) {
// safe to use v.ok here
}
never
function fail(msg: string): never { throw new Error(msg) }
function loopForever(): never { while(true){} }
Exhaustiveness
Assigning to a never
helps enforce exhaustive checks in switch
statements.