TypeScript says the caught error is of type unknown
'err' is of type 'unknown'.Short answer
TypeScript types catch variables as unknown when strict mode (useUnknownInCatchVariables) is on, because anything can be thrown in JavaScript. You cannot read err.message on unknown. Narrow first with instanceof Error, typeof, or a type guard function. The same rule applies to any value typed unknown, such as a function result.
Fix it fast
1Find out which cause you have
Check whether strict mode is on. In TypeScript 4.4 and later, the strict family of options turns on useUnknownInCatchVariables, which makes catch variables unknown.
npx tsc --showConfig | grep -E "strict|useUnknownInCatchVariables"2Match what you see, then jump to its fix
- 1
Property 'message' does not exist on type 'unknown'.The catch variable is unknown under strict modeFix - 2
'obj' is of type 'unknown'. (code 18046) on a property accessA function returns unknown and the result is used directlyFix - 3
Many existing catch blocks fail to compile after turning strict onYou want a different default for catch variablesFix
- 1
What does the error look like?
'err' is of type 'unknown'.Property 'message' does not exist on type 'unknown'.TS18046
Cause 1 of 3
The catch variable is unknown under strict mode
1Check
Look at the catch clause. With the flag on, err is unknown, so reading err.message is an error until you prove it is an Error.
try {
await saveOrder();
} catch (err) {
console.error(err.message); // error
}2Fix
Narrow with instanceof Error before using the properties.
try {
await saveOrder();
} catch (err) {
if (err instanceof Error) {
console.error(err.message);
}
}3Verify
Run the compiler. The error on that line is gone.
npx tsc --noEmitConfirmed by typescriptlang.org
Cause 2 of 3
A function returns unknown and the result is used directly
1Check
A value typed unknown allows no operation until narrowed. This includes your own helper that returns unknown for safety, like a parser.
function safeParse(s: string): unknown {
return JSON.parse(s);
}
const data = safeParse(text);
data.name; // error2Fix
Narrow with typeof and in, or write a type predicate that states the shape.
type User = { name: string };
function isUser(v: unknown): v is User {
return typeof v === 'object' && v !== null && 'name' in v && typeof v.name === 'string';
}
const data = safeParse(text);
if (isUser(data)) {
console.log(data.name);
}3Verify
Run the compiler. The property access inside the guarded block compiles.
npx tsc --noEmitConfirmed by typescriptlang.org
Cause 3 of 3
You want a different default for catch variables
1Check
The flag can be set on its own. TypeScript 4.0 also allowed an explicit annotation on one catch clause.
grep -n "useUnknownInCatchVariables" tsconfig.json2Fix
Prefer narrowing. If you must, annotate a single clause as unknown to be explicit, or turn the flag off in tsconfig.json and accept that err becomes any.
{
"compilerOptions": {
"strict": true,
"useUnknownInCatchVariables": false
}
}3Verify
Run the compiler again.
npx tsc --noEmitConfirmed by typescriptlang.org
How do I stop it coming back?
- Write one small helper that turns an unknown caught value into a message string, and reuse it.
- Keep useUnknownInCatchVariables on: it forces checks that stop crashes when something other than an Error is thrown.
- Return unknown, not any, from parsing helpers and validate the shape at the boundary.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
TypeScript refuses JSX because the jsx option is not set
TS17004: Cannot use JSX unless the '--jsx' flag is provided.
TypeScript compiles JSX only when you name the file with a .tsx extension and enable the jsx compiler option.
2 causes, each with a fixWeb apps
TypeScript rejects a component that returns an array or other non-Element
Its return type 'Element[]' is not a valid JSX element.
TypeScript before 5.1 required every component to return JSX.Element or null, so arrays, strings and Promises were rejected even though React renders them.
2 causes, each with a fixWeb apps
ESLint says it cannot resolve a path to a module that does exist
Unable to resolve path to module 'react'. import/no-unresolved
import/no-unresolved fails when eslint-plugin-import cannot find the file or package on disk with its resolver.
4 causes, each with a fixWeb apps
ESLint reports no-unused-vars for React components and imports that are used
no-unused-vars reported for a component that is rendered as a JSX tag
ESLint's core no-unused-vars does not see variables used only as JSX tags, so a component imported and rendered as <Hello /> looks unused.
3 causes, each with a fixWeb apps
TypeScript rejects MUI Data Grid columns that set a type
A TypeScript error inside a column's renderCell callback
A columns array declared without a type is inferred with type as a plain string, which the Data Grid's column type union does not accept.
2 causes, each with a fixWeb apps
The renderInput prop stopped working on the MUI X DatePicker
The custom TextField (variant, size, helperText) is ignored
renderInput was removed from the MUI X date pickers in version 6.
3 causes, each with a fix