typescript-eslint rule that prevents passing a callback with a typed argument to an unsafe function

Viewed 422

I'm trying to be super strict in my typescript codebase with regards to any. I'm using all the no-unsafe-* rules, so my .eslintrc.js includes:

'@typescript-eslint/no-unsafe-argument': 'error',
'@typescript-eslint/no-unsafe-assignment': 'error',
'@typescript-eslint/no-unsafe-call': 'error',
'@typescript-eslint/no-unsafe-member-access': 'error',
'@typescript-eslint/no-unsafe-return': 'error',

and yet I've just been bitten by an any-caused bug.

Let's suppose an external library has this code:

type Callback = (x: any) => void;

function invokesCallback(callback: Callback): void {
  callback({ thisWill: 'break everything' });
}

and then, in my codebase, I have this:

function appendNumber(value: number) {
  someNumbers.push(value);
}

const someNumbers: number[] = [];
invokesCallback(appendNumber);

I don't see any (pun unintended) static check errors here, and I'd certainly like to!

So, to sum up, I'd like to know if there's a rule which, knowing that the type of a callback is something like (x: any) => void;, only allows functions with this signature to be pass as the callback: (x: unknown) => void;

0 Answers
Related