• Error
  • Reported by the type checker

TS18046

'{0}' is of type 'unknown'.

A value of type `unknown` is used as if it had a known type. Narrow it with a check first.

What it means

unknown means the value could be anything, so the checker won't let you call it, read properties from it or use it in arithmetic until you've narrowed it. Values typed unknown commonly come from catch clauses (with strict) and from parsing external data.

Example

index.ts
export function message(err: unknown) {
  return err.message;
}
ts-r-check -p tsconfig.json --pretty false ts-r output
index.ts(2,10): error TS18046: 'err' is of type 'unknown'.

Recorded by node scripts/verify-examples.mjs with ts-r built from commit 6deaf01. The example uses the default tsconfig: strict, target: es2022, module: esnext, moduleResolution: bundler.

How to fix

  • Narrow first: if (err instanceof Error) return err.message;.
  • Check its shape with typeof, in or a type guard before using it.
  • Validate parsed data against the type you expect rather than asserting it with as.