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

avatar

Borislav Hadzhiev

Last updated: Mar 7, 2022

banner

Photo from Unsplash

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

The error "The left-hand side of an arithmetic operation must be type 'any', 'number', 'bigint' or an enum type" occurs when you have an arithmetic operation with values that are not of type any, number or enum, e.g. a Date. To solve the error convert the values to numbers.

Here is an example of how the error occurs.

index.ts
const date1 = new Date('2023-07-24'); const date2 = new Date('2023-07-23'); // ⛔️ Error: The left-hand side of an // arithmetic operation must be of type // 'any', 'number', 'bigint' or an enum type.ts(2362) const result = date1 - date2;

When you subtract a Date object from another Date object (in JavaScript), they both get implicitly converted to a number that represents the milliseconds elapsed between January 1st, 1970 and the given date.

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

To solve the error, call the getTime() method when subtracting the dates.

index.ts
const date1 = new Date('2023-07-24'); const date2 = new Date('2023-07-23'); 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 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 - like we did with the Date objects.

The only way to solve the error is to make sure the values on the left and right-hand sides of the arithmetic operation are of the aforementioned types - most likely number.

If you aren't getting the error when subtracting Date objects, then try converting the left and right-hand side values of the arithmetic operation to numbers.

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

The example shows how we must convert strings to numbers to be able to subtract them.

Had we not converted the strings to numbers, we would have gotten the same error.

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

This is because we are only able to do arithmetic operations with values of type number, any or enum.

Conclusion #

The error "The left-hand side of an arithmetic operation must be type 'any', 'number', 'bigint' or an enum type" occurs when you have an arithmetic operation with values that are not of type any, number or enum, e.g. a Date. To solve the error convert the values to numbers.

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.