Docs
/
TypeScript
Chapter 2
02 — Primitive Types & Special Types
Core Concepts
- string, number, boolean — the three most common primitives
- any — disables type checking entirely; avoid it
- unknown — type-safe alternative to
any; must narrow before use - void — function returns nothing (undefined implicitly)
- never — function never returns (throws or infinite loop)
- null / undefined — distinct types when
strictNullChecksis on - bigint — arbitrary precision integers (
100n) - symbol — unique identifiers (
Symbol("key")) - literal types — exact values as types (
"hello",42,true)
any vs unknown
| Feature | any | unknown |
|---|---|---|
| Assign anything to it | Yes | Yes |
| Use it without checking | Yes | No |
| Type-safe | No | Yes |
| Recommended | Never | When type is truly unknown |
When to Use
- unknown over any — always; narrow with typeof / instanceof
- never in exhaustive switch checks and unreachable code
- void for callback return types you don't care about
- literal types for strict string/number values (status codes, roles)