ProjectStack
typescript

TS2571: Object is of type 'unknown'

You're trying to access a property or call a method on a value typed as unknown. Unlike any, unknown requires you to narrow the type first — TypeScript won't let you perform operations on unknown values without first confirming what they are.

Common causes

  • Catching an exception in a catch block — TypeScript types the caught error as unknown with useUnknownInCatchVariables
  • A function or API returns unknown and you're using the value without narrowing first
  • Receiving data from an external source typed as unknown to force explicit validation
  • A JSON.parse() result was typed as unknown for safety and isn't narrowed before use

How to fix it

  1. Narrow with typeof: if (typeof value === 'string') { value.toUpperCase() }
  2. Narrow with instanceof: if (error instanceof Error) { error.message }
  3. For catch blocks: if (err instanceof Error) { console.log(err.message) } else { console.log(String(err)) }
  4. Use a type guard function to validate and narrow the type before use

Example

try { riskyOperation(); } catch (err) { console.log(err.message); // error TS2571: Object is of type 'unknown'. }

Accessing .message on the catch parameter, which is typed as unknown in strict mode

Browse more errors

The Developer Hub covers 150+ errors across Git, npm, Node.js, Python, TypeScript, and Docker — with plain-English explanations and fix steps.