Skip to content
Nilay Kabariya

Entry No.113·Fixes··3 min read

Fix: TS18046 'error' is of type 'unknown' in a catch block

Why TS18046 appeared after upgrading to TypeScript 6 or 7 with no config change, tested on 5.9.3 to 7.1 beta, and which fixes pass: instanceof, a helper, any.

The error, verbatim

Jump to the fix ↓
a.ts(1,53): error TS18046: 'error' is of type 'unknown'.
✓ Reproduced on GitHub Actions ubuntu-latest, November 23,

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

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

Useful? Pass it on:Post on XFollow @EmotionalMatter

Related entries

  1. No.109

    Better Auth: Invalid origin, default secret and base URL errors

    Better Auth's Invalid origin, MISSING_OR_NULL_ORIGIN, default secret and base URL errors: 24 setups on 1.7.27 and the status, code and log line each one gave.

    > 403 {"message":"Invalid origin","code":"INVALID_ORIGIN"}

    FIXED4 min
  2. No.023

    Fix: TypeScript enum is not supported in strip-only mode

    Node's ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX for enum, parameter properties, namespace, export = and import =. Two ways to run it unchanged, both tested.

    > enum Color { Red, Green }

    FIXED4 min
  3. No.021

    Fix: typescript-eslint does not support TS 7.0 (ESLint on TS 7)

    When typescript-eslint will support TypeScript 7, why npm refuses the install, and the setup that keeps TypeScript 7 while ESLint runs normally today.

    > npm error code ERESOLVE

    FIXED4 min

tip: paste the exact error text