Running in TypeScript 3.9.7, this does not concern the compiler:
const someFn: () => void = () => 123;
I discovered this answer, which explains that this is by design. I somewhat get the reasoning behind it (basically, you should be able to ignore the return type when passing the function as an argument).
But the story becomes a different one looking at promises:
const someFn: () => void = () =>
new Promise((resolve, reject) => reject(Error('onoez')));
someFn();
I am checking my code with eslint
s @typescript-eslint/no-floating-promises
rule to avoid unhandled promise rejections. In the script above, since the linter thinks someFn
does not return anything, it will not warn me.
Is this something I have to live with? If a function accepts a () => void
type callback, and I pass it an async function, the compiler will not warn me and bad things will start to happen. Can I somehow avoid this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…