On this page
  1. unknown
  2. Syntax & Overview
  3. Notes & Pitfalls
  4. Example

unknown

Prev: builtins | Up: builtins | Next: `never`
Rel: builtins, Structural Typing & Narrowing
Ref: TypeScript Docs — unknown
Tags: #public #web #typescript #builtins #tier/core


Syntax & Overview

const data: unknown = JSON.parse(raw);

Type-safe counterpart to any: accepts any value, but requires narrowing (typeof, type guard) before any property access or call.

Notes & Pitfalls

Example

function parseMessage(err: unknown): string {
  if (err instanceof Error) return err.message;
  if (typeof err === "string") return err;
  return "Unknown error";
}

Linked from

Related notes