简体   繁体   English

如何修复错误类型“void”不可分配给 angular 类型“boolean”?

[英]how to fix the error Type 'void' is not assignable to type 'boolean' in angular?

I am using the following code and getting the error as below:我正在使用以下代码并收到如下错误:

Error: error TS2345: Argument of type '(item: any) => void' is not assignable to parameter of type '(value: any, index: number, obj: any[]) => boolean'.错误:错误 TS2345:“(item: any) => void”类型的参数不可分配给“(value: any, index: number, obj: any[]) => boolean”类型的参数。 Type 'void' is not assignable to type 'boolean'.类型 'void' 不能分配给类型 'boolean'。

Code:代码:

addToCart(product) {
    if (!this.productArr.length) {
      this.productArr.push(product);
    } else {
      let flag = false;
      this.productArr.find((item: any) => {
        if (item.name === product.name) {
          item.count = product.count;
          flag = true;
        }
      });
      if (!flag) {
        this.productArr.push(product);
      }
    }
    this.cartService.addToCart(this.productArr);
  }

Any idea how can we fix it?知道我们该如何解决吗?

addToCart(product) {
    if (!this.productArr.length) {
      this.productArr.push(product);
    } else {
      let flag = false;
      this.productArr.forEach((item: any) => { // change this line
        if (item.name === product.name) {
          item.count = product.count;
          flag = true;
        }
      });
      if (!flag) {
        this.productArr.push(product);
      }
    }
    this.cartService.addToCart(this.productArr);
  }

Change the .find to a .forEach ..find更改为.forEach The .find expects a return of a boolean value of when it found the element you're looking for. .find期望返回一个布尔值,当它找到你正在寻找的元素时。 ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find ) ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find )

You're not returning a value in the arrow function you pass as parameter to find .您没有在作为参数传递给find的箭头函数中返回值。 You need to return a boolean here.您需要在此处返回一个布尔值。

this.productArr.find((item: any) => {
    if (item.name === product.name) {
        item.count = product.count;
        flag = true;
        return true;
    }
    return false;
});

Note that this will also stop further iterations over the array, which may or may not be your intention.请注意,这也将停止对数组的进一步迭代,这可能是您的意图,也可能不是。 Use forEach if you need to find all occurences of item where the name is the same as that of the product.如果您需要查找名称与item名称相同的所有item ,请使用forEach

The better solution would be to use some or every instead of find, depending on whether or not you want to apply this operation to multiple products, since using find implies that you're looking to find something and not performing alterations to an item.更好的解决方案是使用someevery代替 find,这取决于您是否要将此操作应用于多个产品,因为使用find意味着您要查找某些内容而不是对项目进行更改。

// Will stop after the first time `true` is returned
this.productArr.some((item: any) => {
    if (item.name === product.name) {
        item.count = product.count;
        flag = true;
        return true;
    }
});

What about :关于什么 :

addToCart(product: any): void {
    // find will return your product, and it only need a boolean as condition for the arrow function
    const product = this.productArr.find((item: any) => item.name === product.name);

    // If you don't have some products or you don't find your product, you push it
    if (!this.productArr.length || !product) {
      this.productArr.push(product);
    }

    // if you find your product, you update your item.coun
    if (product) {
        item.count = product.count;
    }

    this.cartService.addToCart(this.productArr);
}

Have a look on this article , it explains actually how to the find/map/foreach operators.看看这篇文章,它实际上解释了如何使用 find/map/foreach 操作符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular TypeScript错误:无法将类型“ void”分配给类型“ boolean” - Angular TypeScript error: Type 'void' is not assignable to type 'boolean' 角度错误:类型“void”不可分配给类型“AbstractControl” - Angular Error : Type 'void' is not assignable to type 'AbstractControl' 输入“承诺” <boolean | void> &#39;不可分配给&#39;boolean&#39;类型 - Type 'Promise<boolean | void>' is not assignable to type 'boolean' 在 Angular 中出现错误:输入“布尔值”| 可观察的<boolean> ' 不可分配给类型 'boolean'</boolean> - Getting error in Angular: Type 'boolean | Observable<boolean>' is not assignable to type 'boolean' Angular 7:类型“void[]”不可分配给类型 - Angular 7: Type 'void[]' is not assignable to type Angular - '(error: any) =&gt; void' 类型的参数不可分配给'() =&gt; void' 类型的参数 - Angular - Argument of type '(error: any) => void' is not assignable to parameter of type '() => void' Angular - 如何修复错误 TS2345:'Promise 类型的参数<sweetalertresult> ' 不能分配给 '(value: Object) =&gt; void' 类型的参数</sweetalertresult> - Angular - How to fix error TS2345: Argument of type 'Promise<SweetAlertResult>' is not assignable to parameter of type '(value: Object) => void' 角度承诺 <void> &#39;不可分配给类型 - Angular Promise<void>' is not assignable to type 如何修复错误“Type 'Observable<void | ({…}> ' 不可分配给类型 'EffectResult<action> ’”?</action></void> - How do I fix error “Type 'Observable<void | ({…}>' is not assignable to type 'EffectResult<Action>'”? Angular - 如何在 jasmin 中编写测试真或假 - 错误类型的参数不可分配给预期类型的参数<void></void> - Angular - How to write test true or false in jasmin - Error Argument of type is not assignable to parameter of type Expected<void>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM