[英]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.更好的解决方案是使用some
或every
代替 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.