TypeScript Compiler Diagnostics

TS18046: Value has type unknown

An unknown value must be narrowed before member access. This catch example uses strict-mode unknown catch variables. Not every thrown value is an Error, so the correction handles both Error and non-Error values. Do not assert Error unless the input contract establishes it. Baseline: TypeScript 5.8.3 with the shown configuration. Diagnose the reported location and nested message before applying a fix; other causes may require a different correction. The first project is intentionally invalid; create the corrected project separately. Neither example requires disabling strict checking.

Formal Syntax & Structure

typescript syntaxtypescript
TS18046 (compiler diagnostic code, not TypeScript syntax)

Working Example

Example Snippettypescript
// INTENTIONALLY INVALID — reproduce// File: tsconfig.json (omit this label){  "compilerOptions": {    "strict": true,    "target": "ES2022",    "module": "CommonJS",    "types": [],    "lib": [      "ES2022",      "DOM"    ],    "noEmitOnError": true,    "outDir": "dist"  },  "files": [    "main.ts"  ]}
// File: main.ts (omit this label)try { throw new Error("oops"); } catch (err) { console.log(err.message); }
// Run: tsc -p . --pretty false// Expected: TS18046; no emitted main file.
// CORRECTED PROJECT// File: tsconfig.json (omit this label){  "compilerOptions": {    "strict": true,    "target": "ES2022",    "module": "CommonJS",    "types": [],    "lib": [      "ES2022",      "DOM"    ],    "noEmitOnError": true,    "outDir": "dist"  },  "files": [    "main.ts"  ]}
// File: main.ts (omit this label)try { throw new Error("oops"); } catch (err) { console.log(err instanceof Error ? err.message : String(err)); }
// Run: tsc -p . --pretty false// Expected: no diagnostics.// Run: node dist/main.js// Expected stdout: oops
TypeScript Reference