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

avatar

Borislav Hadzhiev

Last updated: Mar 7, 2022

banner

Photo from Unsplash

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

The error "The right-hand side of an arithmetic operation must be type 'any', 'number', 'bigint' or enum" occurs when you have an arithmetic operation where the right-hand side is not of type any, number or enum, e.g. a string. To solve the error convert the right-hand side to number.

Here is an example of how the error occurs.

index.ts
// ⛔️ Error: The right-hand side of an // arithmetic operation must be of type // 'any', 'number', 'bigint' or an enum type.ts(2363) const result = 5 - '3';

We have an arithmetic operation where the right-hand side is of type string.

However, TypeScript is not happy because it doesn't consider strings a valid left-hand or right-hand side to arithmetic operations.

To solve the error, convert the right-hand side value to a number.

index.ts
const result = 5 - Number('3'); console.log(result); // 👉️ 2

We passed the right-hand side of the arithmetic operation to the Number() function to convert it.

TypeScript only allows us to do arithmetic operations with values of type any, number, bigint or enum.

The error is caused if you try to use any other types in arithmetic operations - e.g. a Date object, string, boolean, etc.

If you are getting the error when subtracting Date objects, convert them to numbers by calling the built-in getTime() method.

index.ts
const date1 = new Date('2022-06-17'); const date2 = new Date('2022-06-16'); const result = date1.getTime() - date2.getTime(); console.log(result); // 👉️ 86400000

The getTime method returns a number that represents the milliseconds between the Unix epoch and the given date.

TypeScript won't let us subtract Date objects, because they don't have a type of number, any or enum, so we have to convert them to numbers before the arithmetic operation.

The error often occurs when using the logical AND (&&) operator or the logical OR (||) operator.

index.ts
// ⛔️ Error: The right-hand side of an // arithmetic operation must be of type // 'any', 'number', 'bigint' or an enum type.ts(2363) // const result = 5 - Number('3'); const result = 4 - (false && 2);

The right-hand side of the operator evaluates to false, so we end up subtracting false from 4 and get the error.

Since booleans are not a valid right-hand side for arithmetic operations, you have to make sure the value on the right-hand side is a number.

Conclusion #

The error "The right-hand side of an arithmetic operation must be type 'any', 'number', 'bigint' or enum" occurs when you have an arithmetic operation where the right-hand side is not of type any, number or enum, e.g. a string. To solve the error convert the right-hand side to a number.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.