wasAsked.

What is the difference between any, unknown and never in TypeScript?

Frequently Asked (All Companies) · Role not specified · technical

Published

Year not specified · TypeScript

Community answers

Posted by wasAsked editorial collection

These three types represent very different ideas. any — "turn off type checking" - A value of type any can be anything, and TypeScript allows any operation on it without checks. let value: any = 'hello'; value.toFixed(2); // no compile error, crashes at runtime value.foo.bar.baz(); // also allowed - any spreads: assigning an any value to typed variables silently removes safety. - Use it only as a temporary escape hatch (migrating JavaScript, poorly typed libraries). Enable noImplicitAny (part of strict mode) and lint rules (no-explicit-any) to avoid accidental any. unknown — "a type-safe any" - Can hold any value, but you cannot use it until you narrow its type with checks. let input: unknown = JSON.parse(text); input.toUpperCase(); // compile error if (typeof input === 'string') { input.toUpperCase(); // OK: narrowed to string } - Ideal for values from external sources: JSON.parse results, API responses, catch clause errors, user input, third-party callbacks. - Narrowing techniques: typeof, instanceof, in operator, Array.isArray, custom type guards, and schema validation: function isUser(x: unknown): x is User { return typeof x === 'object' && x !== null && 'id' in x && 'name' in x; } - With useUnknownInCatchVariables (enabled by strict), catch (e) gives e the type unknown, forcing safe handling: try { ... } catch (e) { const message = e instanceof Error ? e.message : String(e); } never — "a value that can never exist" - The bottom type: no value is assignable to never. - Return type of functions that never complete normally: function fail(message: string): never { throw new Error(message); } function loopForever(): never { while (true) {} } - The result of impossible type operations (string & number is never). - Exhaustiveness checking with discriminated unions: type Shape = { kind: 'circle'; r: number } | { kind: 'square'; side: number }; function area(s: Shape): number { switch (s.kind) { case 'circle': return Math.PI * s.r ** 2; case 'square': return s.side ** 2; default: { const unreachable: never = s; // compile error if a new Shape kind is added and not handled return unreachable; } } } - Used in conditional types to filter members: type NonNullable<T> = T extends null | undefined ? never : T. Comparison - any: everything is allowed; no safety. - unknown: everything can be assigned to it, but you must check before using it; safe. - never: nothing can be assigned to it; represents impossible values and code paths. Also related: void is the return type of functions that return nothing useful (undefined), unlike never, which means the function never returns at all. Interview summary: "any disables type checking; unknown accepts any value but requires narrowing before use, making it the safe choice for external data; never represents values that cannot occur, used for functions that never return and for exhaustive checks."

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics