Why am I getting the error?
I tried various methods, but I did not get the result and this error was not fixed
my code:
function $sum(xor:string[]):string
function $sum(xor:number[]):number
{
let xor_;
if(xor instanceof String){
for(let i of xor){
xor_ =i;
}
}else{
for(let i of xor){
xor_ =i;
}
}
return xor_;
}
my error:
CodePudding user response:
As the error mentions, you must provide a valid implementation signature: Info from the typescript docs
function $sum(xor:string[]):string;
function $sum(xor:number[]):number;
function $sum(xor:Array<string | number>):number | string
{
/**
* as per your overloads the function must return
* - a string when the input is a string array
* - a number when the input is a number array
*
* To determine if the input is an array of string or an array of number
* you could check the type of the first array item
* i.e. typeof xor[0] === 'string'
*
* as T.J.Crowder pointed out in a comment,
* this is not possible when you get an empty array: so your
* function must not allow empty arrays: i.e. throw an error
*/
}
the first 2 lines are the overloads, the 3rd is the implementation signature.