0Pricing
TypeScript Academy · Lesson

unknown vs any: The Safer Choice

Learn why unknown forces you to check types before use.

unknown vs any: The Safer Choice is a free TypeScript Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome

TypeScript 3.0 introduced the `unknown` type as a type-safe alternative to `any`. In this lesson you'll learn what makes `unknown` safer and when to use it.

The Problem with any

With `any`, TypeScript turns off all checks. You can call methods, access properties, and assign to typed variables without restriction — and without safety.
let val: any = getData();
val.someMethod(); // TypeScript: OK. Runtime: possible crash

Introducing unknown

`unknown` is the type-safe top type. Like `any`, it can hold any value. Unlike `any`, you cannot use it without narrowing first.
let val: unknown = getData();
// val.someMethod(); // Error: Object is of type 'unknown'

Narrowing unknown Before Use

To use an `unknown` value, you must narrow it with a type check. TypeScript then allows operations on the narrowed type.
let input: unknown = getInput();
if (typeof input === 'string') {
  console.log(input.toUpperCase()); // OK — narrowed to string
}

unknown Is Not Assignable to Specific Types

You cannot assign `unknown` to a specific type without a check or assertion. This forces you to validate before using the value.
let x: unknown = 'hello';
// let s: string = x; // Error
let s: string = x as string; // OK with assertion

Using unknown in Catch Blocks

In TypeScript 4.0+, caught errors default to `unknown`. This is safer than the old `any` default because it forces you to check the error type.
try {
  doSomething();
} catch (err: unknown) {
  if (err instanceof Error) {
    console.log(err.message); // safe
  }
}

unknown for External Data

When parsing JSON or receiving data from an API, type it as `unknown` first and validate before using.
const raw: unknown = JSON.parse(text);
if (
  typeof raw === 'object' &&
  raw !== null &&
  'name' in raw
) {
  console.log((raw as any).name);
}

Type Guards with unknown

User-defined type guards work perfectly with `unknown`, letting you validate complex shapes before use.
function isUser(val: unknown): val is { name: string } {
  return typeof val === 'object' && val !== null && 'name' in val;
}

unknown vs never

`unknown` is the top type — every type is assignable to it. `never` is the bottom type — nothing is assignable to it. They are opposites.
// unknown: anything can be assigned to it
let a: unknown = 42; // OK

// never: nothing can be assigned to it
function fail(): never { throw new Error(); }

Choosing Between any and unknown

Use `unknown` when you receive data of undetermined type (JSON, user input, external APIs). Use `any` only as a last resort during migration or with untyped libraries.

Runtime Validation Libraries

Libraries like Zod, Valibot, and io-ts pair perfectly with `unknown`. They parse and validate, returning a well-typed value from an `unknown` input.
import { z } from 'zod';
const UserSchema = z.object({ name: z.string(), age: z.number() });
const user = UserSchema.parse(unknownData); // throws if invalid

Quick Check

What must you do before you can use an `unknown` value in TypeScript?

Recap

`unknown` is the safe alternative to `any`. It accepts any value but forces you to narrow the type before use. Use it for external data, catch blocks, and any situation where the type is truly unknown.

Frequently asked questions

Is the “unknown vs any: The Safer Choice” lesson free?

Yes — the full text of “unknown vs any: The Safer Choice” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “unknown vs any: The Safer Choice”?

Learn why unknown forces you to check types before use. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “unknown vs any: The Safer Choice” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Primitive Types: string, number, boolean
  2. The any Type and Why to Avoid It
  3. unknown vs any: The Safer Choice
  4. null, undefined, and Strict Null Checks
← Back to TypeScript Academy