Last updated: Feb 28, 2024
Reading timeยท4 min
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.
Date
objects valid left-hand or right-hand sides to arithmetic operations.getTime()
method when subtracting DateTo 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.
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.
number
.number
is the expected typeIf 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 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.
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.
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 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.
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.
// โ๏ธ 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
.
You can learn more about the related topics by checking out the following tutorials: