diff --git a/packages/eslint-plugin/docs/rules/no-unsafe-call.mdx b/packages/eslint-plugin/docs/rules/no-unsafe-call.mdx index 6fc46d6cbf18..540d464de8ed 100644 --- a/packages/eslint-plugin/docs/rules/no-unsafe-call.mdx +++ b/packages/eslint-plugin/docs/rules/no-unsafe-call.mdx @@ -78,7 +78,7 @@ Note that whereas [no-unsafe-function-type](./no-unsafe-function-type.mdx) helps See, for example, the following code: ```ts -function unsafe(maybeFunction: unknown): string { +function callUnsafe(maybeFunction: unknown): string { if (typeof maybeFunction === 'function') { // TypeScript allows this, but it's completely unsound. return maybeFunction('call', 'with', 'any', 'args'); @@ -87,6 +87,23 @@ function unsafe(maybeFunction: unknown): string { } ``` +In this sort of situation, beware that there is no way to guarantee with runtime checks that a value is safe to call. +If you _really_ want to call a value whose type you don't know, your best best is to use a `try`/`catch` and suppress any TypeScript or linter errors that get in your way. + +```ts +function callSafe(maybeFunction: unknown): void { + try { + // intentionally unsound type assertion + (maybeFunction as () => unknown)(); + } catch (e) { + console.error( + 'Function either could not be called or threw an error when called: ', + e, + ); + } +} +``` + ## When Not To Use It If your codebase has many existing `any`s or areas of unsafe code, it may be difficult to enable this rule.