Was toying with the following code in typescript.
type Summable = string[] | number[];
let a: Summable = ['a', 'b'];
let b: Summable = [1, 2];
let addUp = function (arg: Summable) {
return arg.reduce((a, c) => a + c);
}
The idea was that addUp() would take in an array of numbers or strings and return the sum (if the array was composed of number) or the concatenation (if the array was composed of strings)
Typescript doesnt seem to like Array.reduce method.
This expression is not callable.
Each member of the union type ‘{ (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: string) => string): string; (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: string) => string, initialValue: string): string; (callbackfn: (previousValue: U, currentValue: string, …’ has signatures, but none of those signatures are compatible with each other.ts(2349)
I find this confusing…
At first I took it to mean that.reduce() passed arguments of different types to the callback. However, TS doesnt seem to have any problem with .map() which passes similar arguments.
Any insight would be appreciated.