M Logo
๐Ÿ“ TypeScript from Zero ยท Part 20 of 50

TypeScript any vs unknown, Type Assertions (as) and satisfies

November 6, 2026

Welcome to episode 20 of TypeScript from Zero, a series for complete beginners (no JavaScript needed). Welcome back to TypeScript from zero! Today: what to do when we do not know a type. We stay in the shop project, with a new file called unknowns.ts. Every code block below is the code from the video, ready to copy; run each file to see the output.

Setup

We start in the terminal and open the project in VS Code:

bash
$ touch src/unknowns.ts

1. any

The type any switches the checking off. We say a is any, store a text, and then call to fixed on it, a method of numbers. tsc accepts it, because any allows everything. Node crashes: a dot to fixed is not a function.

ts
let a: any = "hello";
console.log(a.toFixed(2));
bash
$ npx tsc --noEmit
bash
$ node src/unknowns.ts

2. unknown

The safe version is unknown. It also holds any value, but we may not use it until we check its type. We change the one word, and tsc says: a is of type unknown. TS18046.

bash
$ npx tsc --noEmit

3. Narrow

We narrow it with typeof, like in the union episode. Inside the if block a is a string, and tsc is quiet. Node prints the text in capital letters.

ts
if (typeof a === "string") {
  console.log(a.toUpperCase());
}
bash
$ npx tsc --noEmit

4. as

Sometimes we know more than the compiler. The word as is a type assertion: we tell TypeScript to treat a value as a string. It changes no code, it only changes what the compiler believes, so we use it with care.

ts
const raw: unknown = "42";
const text = raw as string;
console.log(text.length);
bash
$ node src/unknowns.ts

5. satisfies

The word satisfies checks a value against a type, but keeps the exact type of the value. The palette must be a record of texts or number lists. And still green is known as a string, and red as a list of numbers, so both methods work.

ts
const palette = {
  red: [255, 0, 0],
  green: "#00ff00",
} satisfies Record<string, string | number[]>;
console.log(palette.green.toUpperCase());
console.log(palette.red.length);
bash
$ node src/unknowns.ts

Recap

Today: any switches checks off, unknown forces a check, narrowing with typeof, as for assertions, and satisfies to check without losing the exact type. Next episode: generics.

ยฉ 2026 All Rights Reserved By Mahmudul Alam

TypeScript any vs unknown, Type Assertions (as) and satisfies - Mahmudul Alam