简体   繁体   English

错误TS2345:类型为'(a:Test,b:Test)=>布尔| 1'不能分配给类型'(a:Test,b:Test)=> number'的参数

[英]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.

相关问题 TS2345:“事件”类型的参数不可分配给“HtmlInputEvent”类型的参数 - TS2345: Argument of type 'Event' is not assignable to parameter of type 'HtmlInputEvent' Angular2 / TypeScript:错误TS2345:类型'String'的参数不能分配给'string'类型的参数 - Angular2/TypeScript: error TS2345: Argument of type 'String' is not assignable to parameter of type 'string' Ngrx/effects:获取错误 TS2345:“Product[]”类型的参数不可分配给“Product”类型的参数 - Ngrx/effects: get error TS2345: Argument of type 'Product[]' is not assignable to parameter of type 'Product' 错误 TS2345:“事件”类型的参数不可分配给“{目标:{值:字符串; }; }' - error TS2345: Argument of type 'Event' is not assignable to parameter of type '{ target: { value: string; }; }' TS2345:“日期”类型的参数 | null' 不可分配给类型为“string |”的参数日期 | 不明确的' - TS2345: Argument of type 'Date | null' is not assignable to parameter of type 'string | Date | undefined' TS2345:“可观察”类型的参数<todointerface[]> ' 不能分配给 'TodoInterface[]' 类型的参数</todointerface[]> - TS2345: Argument of type 'Observable<TodoInterface[]>' is not assignable to parameter of type 'TodoInterface[]' 人员类型的 TypeScript 错误 (TS2345) 参数 | undefined 不能分配给 Person 类型的参数 - TypeScript Error (TS2345) argument of type Person | undefined is not assignable to paramater of type Person 类型..的参数不能分配给Typescript和单元测试中类型..的参数 - Argument of type .. is not assignable to parameter of type .. in Typescript and unit test TS 错误:“null”类型的参数不可分配给“错误”类型的参数 | 承诺喜欢<error | undefined> | 未定义'.ts(2345)</error> - TS error: Argument of type 'null' is not assignable to parameter of type 'Error | PromiseLike<Error | undefined> | undefined'.ts(2345) typescript:类型&#39;any []&#39;的参数不能分配给&#39;[]&#39;类型的参数.ts(2345) - typescript : Argument of type 'any[]' is not assignable to parameter of type '[]'.ts(2345)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM