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

avatar
Borislav Hadzhiev

Last updated: Feb 28, 2024
4 min

banner

# Table of Contents

  1. The LEFT-hand side of an arithmetic operation must be type 'any', 'number', 'bigint' or an enum type
  2. The RIGHT-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type

# 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;

left hand side of arithmetic operation must be type

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.

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

use get time method when subtracting date

The code for this article is available on GitHub

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.

index.ts
const result = Number('5') - Number('3'); console.log(result); // ๐Ÿ‘‰๏ธ 2

convert values to numbers if number is expected type

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.

# 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.

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.

# Convert the right-hand side value to a number

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

index.ts
const result = 5 - Number('3'); console.log(result); // ๐Ÿ‘‰๏ธ 2
The code for this article is available on GitHub

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.

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 code for this article is available on GitHub

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.

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 = 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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev