Imagine a scenario where there is a (silly) function like this:
function doSomething(input: number|string): boolean {
if (input === 42 || input === '42') {
return true;
} else {
return false;
}
}
Question arises as to why array.some()
can be called like this:
function doSomethingWithArray(input: number[]|string[]): boolean {
return input.some(i => doSomething(i));
}
However, calling array.every()
in the same manner results in an error:
function doEverythingWithArray(input: number[]|string[]): boolean {
return input.every(i => doSomething(i));
}
This generates the following error message:
This expression is not callable. Each member of the union type '{ (predicate: (value: number, index: number, array: number[]) => value is S, thisArg?: any): this is S[]; (predicate: (value: number, index: number, array: number[]) => unknown, thisArg?: any): boolean; } | { ...; }' has signatures, but none of those signatures are compatible with each other.
The difference between the two scenarios is puzzling. Logically, both should either work or fail. What could potentially be overlooked?
Note that doSomething()
accepts number|string
as its argument, hence it should operate with every element of number[]|array[]
, similar to how it functions with array.some()
, right?