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 strictNullChecks is on
  • bigint — arbitrary precision integers (100n)
  • symbol — unique identifiers (Symbol("key"))
  • literal types — exact values as types ("hello", 42, true)

any vs unknown

Featureanyunknown
Assign anything to itYesYes
Use it without checkingYesNo
Type-safeNoYes
RecommendedNeverWhen 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)