When working in TypeScript 3.9.7, the compiler is not concerned with the following code:
const someFn: () => void = () => 123;
After stumbling upon this answer, it became apparent that this behavior is intentional. The rationale behind it makes sense (essentially, ignoring the return type when passing the function as an argument).
However, things take a turn when dealing with promises:
const someFn: () => void = () =>
new Promise((resolve, reject) => reject(Error('onoez')));
someFn();
I am using ESLint's
@typescript-eslint/no-floating-promises
rule to catch unhandled promise rejections in my code. In the example above, since the linter believes someFn
does not have a return value, it will not issue a warning.
Do I just have to accept this situation? If a function expects a () => void
type callback and I provide it with an async function, the compiler won't flag any issues, leading to potential problems. Is there a way to prevent this?