catchCallbackTypes
Reports
Promisecatch callback parameters that are not typed as unknown.
✅ This rule is included in the ts logical presets.
TypeScript’s useUnknownInCatchVariables compiler option only affects synchronous catch clauses, not Promise callbacks.
Promise rejection values can be anything, so the callback parameter in .catch() and the second argument to .then() should be typed as unknown to force proper type narrowing before use.
Without an explicit unknown type annotation, the catch callback parameter is implicitly typed as any, undermining type safety.
Examples
Section titled “Examples”Promise.resolve().catch((error) => { console.log(error);});Promise.resolve().catch((error: any) => { console.log(error);});Promise.resolve().then( () => {}, (error) => { console.log(error); },);Promise.resolve().catch((error: unknown) => { if (error instanceof Error) { console.log(error.message); }});Promise.resolve().then( () => {}, (error: unknown) => { console.log(error); },);Promise.resolve().catch(() => { console.log("error occurred");});Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If your codebase is not yet able to enable useUnknownInCatchVariables, it may be similarly difficult to enable this rule.
If you have modified the global type declarations to make then() and catch() callbacks use the unknown type without an explicit type annotation, you do not need this rule.
Further Reading
Section titled “Further Reading”- TypeScript 4.4 Release Notes - useUnknownInCatchVariables
- MDN documentation on Promise.prototype.catch()