The left -hand and right hand side of an arithmetic operation must be of type 'any', 'number' or an enum type
AngularTypescriptAngular Problem Overview
I am getting the following error. I am not able to find out where exactly i went wrong.Can someone help me out with the solution
The code
function() {
this.devices.forEach(device => {
let lastConnect = device.lastConnection.split('+');
lastConnect = lastConnect[0] + 'Z';
let diff = Math.abs(new Date() - new Date(lastConnect));//getting error here
}
Angular Solutions
Solution 1 - Angular
I have found out the issue.
The code you have written works only in Javascript
Math.abs(new Date() - new Date(lastConnect)) .
In order to make it work in Typescript, update the code as shown below:
Math.abs(new Date().getTime() - new Date(lastConnect).getTime());
Solution 2 - Angular
Needless to say, Your code works correctly in Javascript, If you want to get the same result in Typescript, You have 3 options
(1) - With the help of valueOf
let diff = Math.abs(new Date().valueOf() - new Date(lastConnect).valueOf());
(2) - With the help of getTime
let diff = Math.abs(new Date().getTime() - new Date(lastConnect).getTime());
// or
let diff = new Date().getTime() - new Date(lastConnect).getTime();
(3) - With the help of any
let diff = Math.abs(<any>new Date() - <any>new Date(lastConnect))
Solution 3 - Angular
The simplest answer would be
Math.abs(<any>new Date() - <any>new Date(lastConnect));
Solution 4 - Angular
No need for Math.abs()
to answer this question...
Just using getTime()
method converts a Date into a number (Date.prototype.getTime()) so you can make the operation without that error
Solution 5 - Angular
Another great way:
Math.abs((new Date() as any) - (new Date(lastConnect) as any));
Solution 6 - Angular
Typescript seems to have problems with implicit conversions, so we can just make them explicit:
Math.abs(+new Date() - +new Date(lastConnect));
Note that we don't need any parentheses because the new
operator binds tighter than the (unary) +
operator. After all, (+new)
wouldn't make any sense. The +
after the -
is needed as well.
I'd use any
only as a last resort since it basically disables typechecking.
Modifying a piece of code too much just to appease Typescript is not always safe as one may introduce errors because of wrong assumptions. This happens especially when one decides to convert some old .js files into .ts files. I think that making an implicit conversion explicit is the safest approach, in general.
Solution 7 - Angular
I got same issue with *ngIf="event && +event.enableSomething"
, as sometimes I'm receiving "0"
data so parsing is needed but event interface was created for enableSomething: boolean
so in that case I was retriving that error.
To fix this make sure that backend return boolean and leave enableSomething: boolean
in interface or modify an interface to to enableSomething: any
I hope it helps someone with the same problem as occurred on my side.
Solution 8 - Angular
In my case I add .valueOf()
after input of Math.abs()
Math.round(valA.valueOf() / 20)
according to de doc:
> The valueOf()
method returns the primitive value of the specified object.