TypeScript 7 reference · Type Operators and Keywords

TypeScript unknown Keyword

The type-safe 'anything': a value of type unknown must be narrowed (checked) before you can use it, unlike any.

TypeScript 7 Since 3.0

Create a free account to save this page, take notes and track your progress. Sign in

Examples

Narrow before useTry it yourself
function parse(json: string): unknown {
  return JSON.parse(json);
}
const data = parse('{"name":"Ana"}');
// @ts-expect-error: unknown must be narrowed first
data.name;
if (typeof data === "object" && data !== null && "name" in data) {
  console.log(data.name);
}

Output

Ana
Errors in catch are unknownTry it yourself
try {
  throw new Error("boom");
} catch (err: unknown) {
  console.log(err instanceof Error ? err.message : String(err));
}

Output

boom

Version history

VersionChange
TypeScript 3.0Added

Was this page helpful?