any vs unknown vs never (Interview-Ready)

How to choose any/unknown/never, why unknown is safer, and how never appears in narrowing and exhaustive checks.

F

Frontend Interview Team

February 08, 2026

2 min read
any vs unknown vs never (Interview-Ready)

30‑second interview answer

  • any disables type safety (TypeScript stops helping).
  • unknown is a safe top type: you must narrow before using it.
  • never represents an impossible value—often used for exhaustive checks and to model code paths that can’t happen.

any: the escape hatch

let x: any = 123;
x.toUpperCase(); // no error at compile time (but runtime crash)

Use any only when:

  • you’re integrating a truly untyped library
  • you’re in migration mode

Even then, prefer unknown + narrowing.


unknown: safe top type

let value: unknown;
 
// value.toUpperCase(); // ❌ error: can't use unknown directly
 
if (typeof value === 'string') {
  value.toUpperCase(); // ✅
}

Pattern: parse/validate boundary

function parseJson(json: string): unknown {
  return JSON.parse(json);
}
 
const data = parseJson('{"x":1}');
// validate / narrow before using

never: impossible values

1) Exhaustive switch

type Status = 'idle' | 'loading' | 'success' | 'error';
 
function assertNever(x: never): never {
  throw new Error('Unexpected value: ' + x);
}
 
function render(status: Status) {
  switch (status) {
    case 'idle':
    case 'loading':
      return '...';
    case 'success':
      return 'OK';
    case 'error':
      return 'Oops';
    default:
      return assertNever(status); // ✅ if a new Status is added, TS warns
  }
}

2) Narrowing removes possibilities

function fail(msg: string): never {
  throw new Error(msg);
}

Common interview traps

  1. “Why not just use any?”
  • Because it removes correctness guarantees.
  1. “Is unknown the same as any?”
  • No. Unknown forces narrowing.
  1. “Where does never show up in real code?”
  • Exhaustive checks, reducers, discriminated unions.

Mini Q&A

Q1: What’s the difference between any and unknown?

  • unknown is safe; you must narrow.

Q2: What is never used for?

  • exhaustive checks and impossible paths.

Q3: When is any acceptable?

  • temporary migration, truly untyped integration.

Summary checklist

  • I avoid any by default.
  • I use unknown at boundaries.
  • I use never for exhaustive checks.