The error, verbatim
Jump to the fix ↓a.ts(1,53): error TS18046: 'error' is of type 'unknown'.
The fix that worked
Check that it's an Error before reading .message:
catch (error) {
if (error instanceof Error) console.log(error.message)
}Not it? Every cause, tested ↓Tested on
- TypeScript
- 5.9.3, 6.0.3, 7.0.2, 7.1.0-beta
- Node
- 24.22.3
- OS
- GitHub Actions ubuntu-latest, November 23, 2026
Contents
TS18046 says the variable in your catch has the type unknown, so TypeScript won’t let you read .message (or anything else) from it until you check what it is. Anything can be thrown in JavaScript, a string or a plain object as much as an Error, so unknown is the honest type.
The code that triggered it in every failing case was one line:
try { JSON.parse("{") } catch (error) { console.log(error.message) }
a.ts(1,53): error TS18046: 'error' is of type 'unknown'.
Why it showed up after an upgrade
The catch variable is unknown when useUnknownInCatchVariables is on, and strict turns it on. What changed is the default for strict: TypeScript 6.0’s release post says “strict is now true by default” (Announcing TypeScript 6.0). I compiled the line above on four versions:
| Setup | 5.9.3 | 6.0.3 | 7.0.2 | 7.1.0-beta |
|---|---|---|---|---|
tsc a.ts, no tsconfig |
no error | TS18046 | TS18046 | TS18046 |
tsconfig with no strict key |
no error | TS18046 | TS18046 | TS18046 |
tsconfig from tsc --init (it writes "strict": true) |
TS18046 | TS18046 | TS18046 | TS18046 |
"strict": false |
no error | no error | no error | no error |
So a project whose tsconfig never mentioned strict compiled on 5.9 and fails on 6.0 and 7, with no change to its code or config. Setting "strict": false hides this error and every other strict check with it; the fixes below are narrower.
The fixes that passed
Check with instanceof Error (passed on all four versions):
try {
JSON.parse("{")
} catch (error) {
if (error instanceof Error) console.log(error.message)
}A small helper, if you do this in many places (passed on all four):
function getErrorMessage(e: unknown): string {
if (e instanceof Error) return e.message
return String(e)
}
try { JSON.parse("{") } catch (error) { console.log(getErrorMessage(error)) }catch (error: any) also compiled on all four. It turns the check off for that one variable: the compiler no longer stops you reading .message from something that isn’t an Error.
Turn the option off but keep strict (passed on all four):
{ "compilerOptions": { "strict": true, "useUnknownInCatchVariables": false } }catch (error: Error) is a different error
Typing the variable as Error is not allowed:
a.ts(1,39): error TS1196: Catch clause variable type annotation must be 'any' or 'unknown' if specified.
Same message on all four versions. Only any or unknown may be written there.
Two things that look fine and aren’t checked
error.cause works the same way: if (error instanceof Error && error.cause instanceof Error) compiled on all four versions with lib including es2022.
If TS 7 brought other new errors, see the TypeScript 7 migration guide.
How this was tested
GitHub Actions ubuntu-latest, November 23, 2026, Node 24.22.3. Each TypeScript version ran from npx -p typescript@<version> tsc in a fresh folder, with the tsconfig of each case and --noEmit. In the Actions log every compiler error line carries GitHub’s ##[error] prefix, which is left out above. tsc 7.0.2 exited with code 1 on errors; 5.9.3, 6.0.3 and 7.1.0-beta exited 2. Windows and editors (VS Code’s own TypeScript version) were not tested. The script and the output are in nk-repro/ts18046, run 45112034396.
— N.K., end of entry No.113