1 interface Dimensions {
2 width: Number,
3 height: Number
4 }
5
6 function findArea(dimensions: Dimensions): Number {
7 return dimensions.height * dimensions.width;
8 }
line 7, red squiggly lines under dimensions.height
and dimensions.width
The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.
I'm trying to eradicate red squiggly, but I'm stumped as to why the typescript compiler is giving me an error. As far as I can tell, width and height are of type Number.
The Java programming language supports various arithmetic operators for all floating-point and integer numbers. These operators are + (addition), - (subtraction), * (multiplication), / (division), and % (modulo).
Definition. The arithmetic operators perform addition, subtraction, multiplication, division, exponentiation, and modulus operations.
The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. I'm trying to eradicate red squiggly, but I'm stumped as to why the typescript compiler is giving me an error.
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 number.
The right hand side of an arithmetic operation must be of type 'any', 'number' or an enum type 0 Typescript observable The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type 0 The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. Can't declare Number as number 0
Here is another example of this error occurring that might help people.
If you're using typescript and trying to compute the difference between dates (In my case I was attempting to use ISO string from database):
new Date("2020-03-15T00:47:38.813Z") - new Date("2020-03-15T00:47:24.676Z")
TypeScript Playground
It will show the error.
However, this same exact code works if you put it in the browser console or other node environment
new Date("2020-03-15T00:47:38.813Z") - new Date("2020-03-15T00:47:24.676Z")
14137
I may be wrong, but I believe this works due to the -
operator implicitly using valueOf
on each of it's operands. Since valueOf
on Date returns a number
the operation works.
However, typescript doesn't like it. Maybe there is a compiler option for this is forcing this constrain and I'm not aware.
new Date().valueOf()
1584233296463
You can fix by explicitly making the operands number (bigint) types so the -
works.
Fixed Example
new Date("2020-03-15T00:47:38.813Z").valueOf() - new Date("2020-03-15T00:47:24.676Z").valueOf()
TypeScript Playground
Number
should be lowercase: number
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With