简体   繁体   中英

error TS2345: Argument of type '(a: Test, b: Test) => boolean | 1' is not assignable to parameter of type '(a: Test, b: Test) => number'

sorry for long subject, I just don't understand the response.

my code:

this.rezerwacjeFilteredByseaarchInput.sort(function (a, b) {
      if (a[5]===null) {
       // console.log(a[5]);
        return 1;

      }
        if (firmaSortOrder) {
          return a[5] > b[5];
        }
        else {
          return b[5] > a[5];
        }

    });

js throws: error TS2345: Argument of type '(a: Rezerwacja, b: Rezerwacja) => boolean | 1' is not assignable to parameter of type '(a: Rezerwacja, b: Rezerwacja) => number'. Type 'boolean | 1' is not assignable to type 'number'. Type 'true' is not assignable to type 'number'.

According to MDN description of sort function , sort compare function has to return a number. Your first condition returns number, but other two returns boolean values. Below code should work.

this.rezerwacjeFilteredByseaarchInput.sort(function (a, b) {
    if (a[5] === null) {
        return 1;
    }
    if (firmaSortOrder) {
        return a[5] - b[5];
    }
    return b[5] - a[5];
});

This is a typescript compilation error. the signature in rezerwacjeFilteredByseaarchInput is incorrect. It should only return number values (this is what the message is saying ) but in its body you can read that it can return the boolean values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM