Skip to content

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.

Rajesh DhimanFebruary 08, 20262 min read
On this page

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.
Companion Book

Looking for structured, offline interview prep?

Free online lessons • Portable Amazon Kindle / Paperback

Frontend Interview Guide (2025 Edition) cover
Recommended for React & Frontend Rounds

Frontend Interview Guide (2025 Edition)

By Rajesh Dhiman

Comprehensive preparation covering React internals, Web Performance, CSS architecture, and system design questions.

View on Amazon ↗Kindle & Paperback
JavaScript Interview Guide cover
Recommended for Core JS Rounds

JavaScript Interview Guide

By Rajesh Dhiman

Deep dive into the Event Loop, microtask draining, closures, prototypal inheritance, and asynchronous execution patterns.

View on Amazon ↗Kindle & Paperback

Note: All lessons on Frontend Interview remain completely free to read online. As an Amazon Associate, we may earn from qualifying purchases.

RD

Written by Rajesh Dhiman

Authored by Rajesh Dhiman and engineered at Eunix Tech. Focused on deep-dive frontend engineering, JavaScript runtime internals, React architecture, and web performance.