- Error
- Reported by the type checker
TS1196
Catch clause variable type annotation must be 'any' or 'unknown' if specified.
A `catch` variable is annotated with a type other than `any` or `unknown`. Remove the annotation or use `unknown` and narrow it.
What it means
Anything can be thrown in JavaScript, so a catch clause can't promise a specific type for the caught value. Only any and unknown are allowed as annotations.
Example
try {
JSON.parse('{');
} catch (e: Error) {
console.log(e);
}
export {};
ts-r-check -p tsconfig.json --pretty false ts-r outputindex.ts(3,13): error TS1196: Catch clause variable type annotation must be 'any' or 'unknown' if specified.
Recorded by node scripts/verify-examples.mjs with ts-r built from commit 6deaf01. The example uses the default tsconfig: strict, target: es2022, module: esnext, moduleResolution: bundler.
How to fix
- Annotate with
unknown(or leave it out) and narrow:if (e instanceof Error) { ... }.