unknown
unknown is the type for a value you don't know yet: you can hold it, but you must check what it is before you use it.
unknown says: "this could be anything, and I don't know which yet". Any value can be assigned to an unknown variable. But the type checker won't let you use it (call methods, add, read properties) until you prove its type with a check like typeof or instanceof. Inside that check, TypeScript narrows the type and you can work with it.
That is why the course uses it for data from outside, as in const fromOutside: unknown = JSON.parse("42");. You can't trust that data, so it forces you to verify it.
Unlike any, which switches checking off, unknown keeps it on. The type exists only for the type checker: it is gone when Node runs the code.
Reach for it when: you receive a value whose type you don't control: JSON, user input, or an error caught in a `catch`.
Examples
Common mistake
const data: unknown = JSON.parse("\"hello\"");
console.log(data.toUpperCase());Why: TypeScript doesn't know what `data` is, so it won't let you call methods on it.
How to write it: Check first: if (typeof data === "string") { data.toUpperCase(); }.
Common mistake
const value: unknown = 5; const n: number = value; console.log(n);
Why: An `unknown` can't be assigned to a concrete type without checking it.
How to write it: Narrow with typeof value === "number" before assigning it.
