On this page
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
- Type-safe top type (always prefer over
any): Accepts any value, but forbids property access or calls until narrowed withtypeof,instanceof, or a schema validator.
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
- tuts/typescript/builtins/never Linked here; links to this page; shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins Linked here; links to this page; shared tags: #builtins, #typescript, #web
- tuts/typescript/type_system/structural_typing_and_narrowing Linked here; links to this page; shared tags: #typescript, #web
- tuts/typescript/builtins/any links to this page; shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/tutorials/type_safe_api_clients_and_validation links to this page; shared tags: #typescript, #web
- tuts/typescript/builtins/as_const shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins/asserts shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins/enum shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins/infer shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins/is shared tags: #builtins, #tier, #typescript, #web