When doing a sort in typescript I get a git bash compiler type error -
when doing sort in typescript git bash error code works fine
i'm using webpack angular version 4 typescript in angular cli.
when doing ng serve typescript arithmetic operator error.
the sort function has issue below. sort code looks fine in terms of js when in ts doesnt compile.
here's code works fails when compiling in typescript:
getalljobs = (): void => { this.recentjobs = []; let alljobs = []; this.jobservice.getalljobs().then((alljobs: any) => { //error here - sort error here in typescript compiler alljobs.sort((a: any, b: any) => { return new date(b.sortdate) - new date(a.sortdate); }); }); }
typescript complains because thinks since date
object , using arithmetic operators, incompatible. happens in javascript when (new date(b.sortdate)).valueof() - (new date(a.sortdate)).valueof();
new date().valueof()
returns number.
you can use +
operator coerce date number , comparison:
alljobs.sort((a: any, b: any) => { return (+new date(b.sortdate)) - (+new date(a.sortdate)); });
or use new date().valueof()
returns number:
alljobs.sort((a: any, b: any) => { return (new date(b.sortdate)).valueof() - (new date(a.sortdate)).valueof(); });
here's similar issue on typescript repository might of interest you: https://github.com/microsoft/typescript/issues/5710
Comments
Post a Comment