Sometimes, we want to fix the "The left -hand and right hand side of an arithmetic operation must be of type ‘any’, ‘number’ or an enum type" error with TypeScript.
In this article, we’ll look at how to fix the "The left -hand and right hand side of an arithmetic operation must be of type ‘any’, ‘number’ or an enum type" error with TypeScript.
How to fix the "The left -hand and right hand side of an arithmetic operation must be of type ‘any’, ‘number’ or an enum type" error with TypeScript?
To fix the "The left -hand and right hand side of an arithmetic operation must be of type ‘any’, ‘number’ or an enum type" error with TypeScript, we should make sure all the operands we use with arithmetic operators are numbers.
For instance, if we’re subtracting 2 timestamps, we should make sure they’re both timestamps by calling the getTime
method with the Date
instances.
So we write
const d = Math.abs(new Date().getTime() - new Date(lastConnect).getTime());
to call getTime
with both Date
instances to return the timestamps before we subtract them.
Conclusion
To fix the "The left -hand and right hand side of an arithmetic operation must be of type ‘any’, ‘number’ or an enum type" error with TypeScript, we should make sure all the operands we use with arithmetic operators are numbers.
For instance, if we’re subtracting 2 timestamps, we should make sure they’re both timestamps by calling the getTime
method with the Date
instances.