Return Types of Callbacks
Don’t use the return type any
for callbacks whose value will be ignored:
1 2 3 4 | /* WRONG */ function fn(x: () => any) { x(); } |
Do use the return type void
for callbacks whose value will be ignored:
1 2 3 4 | /* OK */ function fn(x: () => void) { x(); } |
Why: Using void
is safer because it prevents you from accidently using the return value of x
in an unchecked way:
1 2 3 4 | function fn(x: () => void) { var k = x(); // oops! meant to do something else k.doSomething(); // error, but would be OK if the return type had been 'any' } |
Please login to continue.