The left-hand side of an arithmetic operation must be type ‘any’, ‘number’, ‘bigint’ or an enum type – How to fix it?

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

Knowing how to solve the error “The left-hand side of an arithmetic operation must be type ‘any’, ‘number’, ‘bigint’ or an enum type” will help you to know more about how to work with the arithmetic operator. So how to do it? Let’s go into detail now.

The reason for the error “The left-hand side of an arithmetic operation must be type ‘any’, ‘number’, ‘bigint’ or an enum type”

This error happens when you use an arithmetic operation with a value that does not type ‘number’, ‘enum’, ‘bigint’… etc.

Example of how this error happens:

const dateType = new Date();

const anObj = {
    name: "Garett Crooks",
    age: 23,
    eye: "colorBlue",
    hair: "HairWavy, Black",
};

const err = anObj - dateType;

Error:

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

Here I get the error because on the left-hand side of the arithmetic operation, I am using object type, and the right-hand side is date type, so it is not allowed in Typescript.

Or with string type:

Example:

const theError = '10' - '9'

The error:

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

Here I also get the error when using arithmetic operations with string type.

You can read more about basic type here.

Solutions for this error

Convert to the correct type

The obvious solution here is to convert all wrong types that get the error to the correct type, which is the number type.

With date type, you can convert the value to number type by converting time to milliseconds or only using a particular date, month, or year.

Example:

const dateNow= new Date()
const anotherDate = new Date('2022-1-5');

// GetTime method will convert time to milisecond
const milisecond = dateNow.getTime() - anotherDate.getTime()
console.log(milisecond)

// GetYear method will return year
const year = dateNow.getFullYear() - anotherDate.getFullYear()
console.log(year)

// GetMonth method will return month
const month = dateNow.getMonth() - anotherDate.getMonth()
console.log(month)

// GetDate method will return date
const date = dateNow.getDate() - anotherDate.getDate()
console.log(date)

Output:

[LOG]: 25519852635   
[LOG]: 0 
[LOG]: 9 
[LOG]: 22 

Use Number constructor

You can use the Number constructor to convert some specific string to the number type.

Example:

const value = Number('10') - Number('9')
console.log(value)

Output:

[LOG]: 1

Summary

In this article, I showed you how to fix the error “The left-hand side of an arithmetic operation must be type ‘any’, ‘number’, ‘bigint’ or an enum type”. When working with the arithmetic operation, you should convert your wrong type to some correct type, like a number.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *