# Solved- Adding two numbers concatenating them in TypeScript

July 12, 2022

## Adding two numbers concatenating them in TypeScript:

This is a common error in TypeScript. If you are trying to add two numbers, it concatenates the numbers. The common reason for this issue is that one of the numbers is not a number. If it is a string, it will concatenate the other number to this string. Or if both numbers are strings, it will concatenate them.

### Example program with this error:

Let’s take a look at the below example:

``````function getSum(first: any, second: any) {
return first + second;
}

console.log(getSum(10, 20));
console.log(getSum("10", 20));``````

In this example, getSum method takes two parameters. Both of these parameters are of type any. It returns the value of first + second.

For the first example, this method is called with two numbers. For the second example, it is called with one string and one number.

If you run this program, it will print:

``````30
1020``````

For the first example, it returns 30, and for the second example, it returns 1020. It concatenates the values in the second example.

There are two issues with this program:

• The parameters are any type. So, this function will not throw any compile-time error if we pass numbers or strings.
• It is using + to add the values of the parameters. But, since it can accept any type, it will concatenate or add the values.

### Solution 1: By using union type:

One way to fix this issue is to use union type. Instead of any, we can use number | string i.e. it can accept number or string values. If it is a string, we can check it by using typeof and convert it to a number using Number.

``````function getSum(first: number | string, second: number | string) {
if (typeof first === "string") {
first = Number(first);
}

if (typeof second === "string") {
second = Number(second);
}
return first + second;
}

console.log(getSum(10, 20));
console.log(getSum("10", 20));``````

It will print 30 for both.

### Solution 2: By using + operator:

We can also use the + operator to convert a string to number. It will look as like below:

``````function getSum(first: number | string, second: number | string) {
if (typeof first === "string") {
first = +first;
}

if (typeof second === "string") {
second = +second;
}
return first + second;
}

console.log(getSum(10, 20));
console.log(getSum("10", 20));``````

It will also print the same result.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7715489,"math_prob":0.97356856,"size":2346,"snap":"2023-14-2023-23","text_gpt3_token_len":572,"char_repetition_ratio":0.15627669,"word_repetition_ratio":0.16009852,"special_character_ratio":0.27450982,"punctuation_ratio":0.17355372,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9933803,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-06-05T17:34:02Z\",\"WARC-Record-ID\":\"<urn:uuid:5e5c0b74-9eaa-447e-ba49-80bdfa6d4a5b>\",\"Content-Length\":\"54717\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:2e1ef324-eab5-4a59-adc9-d97acb9f97d8>\",\"WARC-Concurrent-To\":\"<urn:uuid:84ebd8e7-d5b4-468e-8a3b-ad7c8c6641e0>\",\"WARC-IP-Address\":\"18.213.98.197\",\"WARC-Target-URI\":\"https://nkaushik.com/typescript/typescript-adding-numbers-concatenating/\",\"WARC-Payload-Digest\":\"sha1:HLT7AW7QPTHPJFGCHJ4NUYVZWHUEK3YC\",\"WARC-Block-Digest\":\"sha1:F6RKAAT535TOXMMNWIOVA3WLKNIIRKT7\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-23/CC-MAIN-2023-23_segments_1685224652149.61_warc_CC-MAIN-20230605153700-20230605183700-00074.warc.gz\"}"}