ReviseAlgo Logo

TypeScript Fundamentals

Any, Unknown, Never, Void

Master TypeScript's top and bottom types: any, unknown, void, and never, type narrowing strategies, exhaustiveness checking, and strict type safety.

Last Updated: July 29, 2026 10 min read

TypeScript provides special top and bottom types to represent dynamic values, unvalidated data, missing return values, and unreachable states.

1. any: Opting Out of Type Checking

The any type disables static type checking for a variable. You can call any method, access any property, or reassign it to any value.

Warning: Excessive use of any defeats the purpose of TypeScript. Prefer unknown for dynamic or unknown input.

2. unknown: The Type-Safe Top Type

unknown accepts any value (like any), but TypeScript forbids operating on an unknown value without first narrowing its type via type guards.

3. void: Return Type for Functions Returning Nothing

void indicates that a function does not return a value (or explicitly returns undefined).

4. never: The Bottom Type (Unreachable Code)

never represents values that never occur. It is used as the return type for functions that throw errors or enter infinite loops, or to enforce exhaustiveness checking.

Functions that Never Return

Exhaustiveness Checking in Switch Statements

5. Interactive Code Playground

Compare unknown vs any in action:

6. Common Pitfalls & Edge Cases

  • any Infects Downstream Code: Assigning an any value to another variable removes type safety for that variable as well.
  • void vs undefined:
  • - A function returning void can return undefined. - A function annotated as returning undefined MUST explicitly return undefined;.

    7. Special Types Summary Table

    TypeAssignable FromCan Assign ToPrimary Purpose
    anyEverythingEverythingOpt out of type checking (Legacy migration)
    unknownEverythingOnly unknown and anySafe unvalidated data (APIs, JSON parsing)
    voidundefined, voidany, unknownFunction with no return value
    | never | Nothing | Everything | Unreachable states & exhaustive checking |