I find the book Learning JavaScript to be confusing. It delivers conflicting information regarding the use of void
as a return type in functions.
const foo = (s: string): void => {
return 1; // ERROR
}
At first, it states that if a function has a return type of void
, it cannot return anything. But then it contradicts itself by explaining that void
means the return type should be ignored, allowing functions like:
arr.forEach(foo);
This code snippet uses a function foo
with a supposed return type of void
, even though it does return something.
To further illustrate this contradiction, consider the following example:
type Foo = (s: string) => number;
const foo: Foo = (s) => {
return 1;
}
function bar(cb: (s: string) => void): boolean {
return true;
}
bar(foo);
In this scenario, the function cb
is expected to have a return type of void
, but it can actually return a value without any issues.
The conflicting explanations from the book leave me puzzled about how these discrepancies can be reconciled.