Skip to content
Latchkey

tsc TS18046: 'x' is of type 'unknown' in CI

TS18046 fires when you operate on a value typed unknown without narrowing it. Under strict mode, caught error variables are unknown, so reading err.message directly is an error.

What this error means

tsc fails with "error TS18046: 'err' is of type 'unknown'." inside a catch block or anywhere an unknown value is used before being narrowed.

tsc
src/run.ts:6:25 - error TS18046: 'err' is of type 'unknown'.

6   console.error('failed', err.message);
                            ~~~

Common causes

A catch variable is unknown under strict mode

useUnknownInCatchVariables (on under strict) types caught errors as unknown, so accessing properties without a guard is an error.

An unknown value used without narrowing

Values from JSON.parse, generic boundaries, or APIs typed unknown must be narrowed before use.

How to fix it

Narrow the value before using it

  1. Check the value with instanceof or a type guard.
  2. Access properties only inside the narrowed branch.
  3. Re-run tsc to confirm the unknown is handled.
src/run.ts
try {
  run();
} catch (err) {
  const message = err instanceof Error ? err.message : String(err);
  console.error('failed', message);
}

Use a typed helper to extract a message

Centralize narrowing in a helper so catch blocks stay clean and consistent.

src/errors.ts
function toMessage(e: unknown): string {
  return e instanceof Error ? e.message : String(e);
}

How to prevent it

  • Narrow caught errors with instanceof Error before use.
  • Treat unknown from parsing and APIs as needing a guard.
  • Keep strict (and useUnknownInCatchVariables) consistent across configs.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →