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.
- All episodes are in the TypeScript from Zero playlist.
- Versions used: Node.js 26.13.0, npm 11.20.0, TypeScript 7.0.2.
Setup
We start in the terminal and open the project in VS Code:
$ touch src/unknowns.ts1. 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.
let a: any = "hello";
console.log(a.toFixed(2));$ npx tsc --noEmit$ node src/unknowns.ts2. 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.
$ npx tsc --noEmit3. 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.
if (typeof a === "string") {
console.log(a.toUpperCase());
}$ npx tsc --noEmit4. 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.
const raw: unknown = "42";
const text = raw as string;
console.log(text.length);$ node src/unknowns.ts5. 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.
const palette = {
red: [255, 0, 0],
green: "#00ff00",
} satisfies Record<string, string | number[]>;
console.log(palette.green.toUpperCase());
console.log(palette.red.length);$ node src/unknowns.tsRecap
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.