[英]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 | js抛出:错误TS2345:类型为'(a:Rezerwacja,b:Rezerwacja)=>布尔值| 1' is not assignable to parameter of type '(a: Rezerwacja, b: Rezerwacja) => number'. 1'不能分配给类型'(a:Rezerwacja,b:Rezerwacja)=> number'的参数。 Type 'boolean | 输入'boolean | 1' is not assignable to type 'number'. 1'不可分配给'number'类型。 Type 'true' is not assignable to type 'number'. 类型“ true”不可分配给“数字”类型。
According to MDN description of sort function , sort compare function has to return a number. 根据MDN对排序功能的描述 ,排序比较功能必须返回一个数字。 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. rezerwacjeFilteredByseaarchInput中的签名不正确。 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. 它应该只返回数字值(这就是消息所要表达的意思),但是在其主体中您可以看到它可以返回布尔值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.