Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

typescript

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.

like image 491
jmrah Avatar asked Apr 11 '16 23:04

jmrah


People also ask

What type of arithmetic operators are used with numeric type?

The Java programming language supports various arithmetic operators for all floating-point and integer numbers. These operators are + (addition), - (subtraction), * (multiplication), / (division), and % (modulo).

What are the types of arithmetic operators?

Definition. The arithmetic operators perform addition, subtraction, multiplication, division, exponentiation, and modulus operations.

What is the left-hand side of an arithmetic operation?

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.

How to solve the error “the right-hand side of an arithmetic operation”?

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.

What are the rules for arithmetic operations in typescript?

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


2 Answers

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. enter image description here

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

like image 90
Matt Mazzola Avatar answered Oct 18 '22 00:10

Matt Mazzola


Number should be lowercase: number.

  • See sample on Playground
  • Documentation for basic types
like image 74
BrunoLM Avatar answered Oct 17 '22 23:10

BrunoLM