Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Receive 2394 in typescript

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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:

error

>Solution :

As the error mentions, you must provide a valid implementation signature:
Info from the typescript docs

Stackblitz example:

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading