No Catch Unknown Annotation
Remove redundant : unknown annotations from catch clause variables because TypeScript already defaults them to unknown.
Summary
The no-catch-unknown-annotation rule reports any catch clause parameter that has an explicit : unknown type annotation. Since TypeScript 4.4+ defaults catch variables to unknown (when useUnknownInCatchVariables or strict is enabled), the annotation adds no value and should be removed for cleanliness.
Why Use This Rule?
- Eliminates noise from redundant type annotations that TypeScript already enforces.
- Keeps catch clauses consistent across the codebase.
- Reduces the chance of cargo-culting an annotation that suggests the type might be something other than
unknown.
Examples
- Correct
- Incorrect
try {
riskyOperation();
} catch (error) {
// error is already unknown by default.
console.error(error);
}
try {
riskyOperation();
} catch {
// Omitting the parameter entirely is also fine.
}
try {
riskyOperation();
} catch (error: unknown) {
// The `: unknown` annotation is redundant.
console.error(error);
}
Configuration
Enable the rule in your ESLint flat config:
import { noCatchUnknownAnnotation } from '@cbnventures/nova/rules/eslint';
export default [
{
plugins: {
'@cbnventures/nova': {
rules: {
'no-catch-unknown-annotation': noCatchUnknownAnnotation,
},
},
},
rules: {
'@cbnventures/nova/no-catch-unknown-annotation': ['warn'],
},
},
];
This rule has no additional options.
Troubleshooting
- Warning fires but you want to keep the annotation for documentation. — It is recommended to remove it and rely on TypeScript's built-in default. The annotation is purely redundant.
- Using an older TypeScript version. — If your project uses TypeScript below 4.4 (or does not enable
useUnknownInCatchVariables/strict), this rule does not apply since catch variables default toanyin that configuration.