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
The Problem with any
let val: any = getData();
val.someMethod(); // TypeScript: OK. Runtime: possible crashIntroducing unknown
let val: unknown = getData();
// val.someMethod(); // Error: Object is of type 'unknown'Narrowing unknown Before Use
let input: unknown = getInput();
if (typeof input === 'string') {
console.log(input.toUpperCase()); // OK — narrowed to string
}unknown Is Not Assignable to Specific Types
let x: unknown = 'hello';
// let s: string = x; // Error
let s: string = x as string; // OK with assertionUsing unknown in Catch Blocks
try {
doSomething();
} catch (err: unknown) {
if (err instanceof Error) {
console.log(err.message); // safe
}
}unknown for External Data
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
function isUser(val: unknown): val is { name: string } {
return typeof val === 'object' && val !== null && 'name' in val;
}unknown vs never
// 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
Runtime Validation Libraries
import { z } from 'zod';
const UserSchema = z.object({ name: z.string(), age: z.number() });
const user = UserSchema.parse(unknownData); // throws if invalidQuick Check
Recap
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
- Primitive Types: string, number, boolean
- The any Type and Why to Avoid It
- unknown vs any: The Safer Choice
- null, undefined, and Strict Null Checks