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
- Check the value with
instanceofor a type guard. - Access properties only inside the narrowed branch.
- 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 Errorbefore use. - Treat
unknownfrom parsing and APIs as needing a guard. - Keep
strict(and useUnknownInCatchVariables) consistent across configs.
Related guides
tsc TS18048: 'x' is possibly 'undefined' (strictNullChecks) in CIFix tsc "error TS18048: 'x' is possibly 'undefined'" in CI - under strictNullChecks tsc requires a guard befo…
tsc TS2531: Object is possibly 'null' in CIFix tsc "error TS2531: Object is possibly 'null'" in CI - under strictNullChecks you accessed a member on a v…
tsc TS2339: Property 'x' does not exist on type 'Y' in CIFix tsc "error TS2339: Property 'x' does not exist on type 'Y'" in CI - you accessed a member that is not dec…