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.

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 sides to arithmetic operations.

Use the getTime() method when subtracting Date

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

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.

The allowed values for arithmetic operations in TypeScript

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.

Convert the values to Numbers if number is the expected type

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

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.

// ⛔️ 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.

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 a number.

Here is an example of how the error occurs.

// ⛔️ 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.

Convert the right-hand side value to a number

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

const result = 5 - Number('3');

console.log(result); // 👉️ 2`

We used the Number() constructor to convert the string to a number.

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.

Solve the error when working with Dates

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

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 logical OR (||) and logical AND (&&) operators

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

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

The right-hand side of the operator evaluates to false, so we end up subtracting false from 4, which caused 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.