There are many reasons of the error “The right-hand side of an arithmetic operation must be of type ‘any’, ‘number’, ‘bigint’ or an enum type”, such as you are using an arithmetic operation between string and number type. So how to solve it? Read this article to find the answer.

## The main reason of the “The right-hand side of an arithmetic operation must be of type ‘any’, ‘number’, ‘bigint’ or an enum type” error

To use an arithmetic operation, both of the two expressions must have ‘any’, ‘number’, ‘bigint’ or an enum type. The error will occur if we use the wrong type, like a string on the right-hand side.

## Solution to fix the “The right-hand side of an arithmetic operation must be of type ‘any’, ‘number’, ‘bigint’ or an enum type” error

**Between number and string type**

Of course, if you do number type minus string type, the error will happen, and we won’t have any ways to solve it. But if your right-hand side expression is something like user input and is a number in string type, we can convert it to a number to solve the error.

Example of how the error happens:

//Use arithmetic operation between number and string type const exp = 10 - ''10''

Error:

`The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.`

**The solution:**

// Use number constructor to convert string to number type const exp = 10 - Number("10"); console.log(exp);

**Output:**

`[LOG]: 0 `

This way can only use with the number in string type. If you try to use it with some strings that have the number but contain the string, then it won’t work.

**Example:**

//Use number constructor to convert string to number type const str: string = "10ten"; const exp = 10 - Number(str); console.log(Number(str)); console.log(exp);

**Output:**

```
[LOG]: NaN
[LOG]: NaN
```

**Between date and number type**

If you want to calculate the time with date type, you can convert it to milliseconds by the getTime method to do it. The getTime method will return the number of the second from the date to the beginning of 1970/1/1.

Example of how the error happens:

const day1 = new Date("2022-12-10"); const day2 = new Date("2022-12-5"); const result = day1 - day2; // ERROR console.log(result);

Error:

```
The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
```

**The solution:**

const day1 = new Date("2022-12-10"); const day2 = new Date("2022-12-5"); // Use the getTime method to convert time to millisecond const date1InMil = day1.getTime(); const date2InMil = day2.getTime(); const result = date1InMil - date2InMil; console.log(result);

**Output:**

`[LOG]: 457200000 `

**Create type guard**

If you cannot manipulate and predict what type will be passed, you can make a code block to check if the value is valid like a type guard.

**Example:**

const str: string = "Typescript"; const num1: number = 10; const num2: number = 10; // Using if/else statement as a type guard function minusValue(value1: any, value2: any) { if (typeof value1 == "number" && typeof value2 == "number") { return value1 - value2; } else { return "Invalid type"; } } console.log(minusValue(num1, str)); console.log(minusValue(num1, num2));

**Output:**

```
[LOG]: "Invalid type"
[LOG]: 0
```

You can read more about number type here.

**Summary**

In this article, I showed you how to solve the error “The right-hand side of an arithmetic operation must be of type ”any”, ”number”, ”bigint” or an enum type”. I recommend you always manipulate the type of value and the value you work with by type guard. It can make you reduce the bug you get. Gook lucks!

**Maybe you are interested**:

- The left-hand side of an arithmetic operation must be type ‘any’, ‘number’, ‘bigint’ or an enum type
- Yield expression implicitly results in an ‘any’ type in TypeScript
- This expression is not callable. Type ‘Number’ has no call signatures

Hello, guys! I hope that my knowledge in HTML, CSS, JavaScript, TypeScript, NodeJS, ReactJS, MongoDB, Python, MySQL, and npm computer languages may be of use to you. I’m Brent Johnson, a software developer.

**Name of the university: **HOU

**Major**: IT

**Programming Languages**: HTML, CSS, JavaScript, TypeScript, NodeJS, ReactJS, MongoDB, PyThon, MySQL, npm