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
- Narrow with typeof: if (typeof value === 'string') { value.toUpperCase() }
- Narrow with instanceof: if (error instanceof Error) { error.message }
- For catch blocks: if (err instanceof Error) { console.log(err.message) } else { console.log(String(err)) }
- 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.
