Lesson 04 of 55
9 mins readPython Top & Bottom Types: any, unknown & never
In Plain English
`any` turns off all type checking. `unknown` is the type-safe top type (requires narrowing before use). `never` is the bottom type representing values that never occur.
Deep Dive: How It Works
any (Unsafe): Disables compiler checks, allowing any method call or property access.
unknown (Safe Top Type): Represents any value, but TypeScript forbids operations until narrowed with `typeof` or `instanceof`.
never (Bottom Type): Represents unreachable code, exhaustive switch checks, or functions that always throw.
Core Rules to Remember

Prefer unknown over any: `unknown` forces callers to check types before using values.

never for Exhaustiveness: Guarantees all branches of a discriminated union are handled.
Live Interactive Example
Hit Run Code to see it liveSafely Narrowing unknown Values
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
Output Console
Click "Run Code" to view the rendered output.
How it works: typeof checks safely narrowed unknown to string and number.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Narrow Unknown Input
Write `function doubleInput(val: unknown): number`.
If `typeof val === "number"`, return `val * 2`. Otherwise return `0`.
Log `doubleInput(21)`.
1
2
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.