[英]Angular TypeScript error: Type 'void' is not assignable to type 'boolean'
what is wrong with the following code. 以下代码有什么问题。 it complains that the void is not assignable to boolean for const invalidResult.
它抱怨不能将const invalidResult的void分配给boolean。
isSubmitDisabled(): boolean {
const results: Result[] = this.getLFResults();
const invalidResult: boolean = results.forEach(function(result) {
if (result.indicators.length === 0) {
return true;
}
return false;
});
return results.length === 0 || invalidResult;
// return results.length === 0;
}
getLFResults(): Result[] {
if (!this.logframe) {
const results: Result[] = [];
this.store.select(getCurrentLogFrameState)
.map((logframe) => {
this.logframe = logframe;
})
.catch((err) => {
return results;
});
if (this.logframe !== null && this.logframe !== undefined) {
return this.logframe.results;
} else {
return results;
}
}
}
The forEach method doesn't return a value, the signature of the method is: forEach方法不返回值, 该方法的签名为:
forEach(callbackfn: (this: void, value: T, index: number, array: T[]) => void): void;
You probably want to use the every method which returns a boolean: 您可能想使用返回布尔值的every方法 :
results.every(function (result) {
if (result.indicators.length === 0) {
return true;
}
return false;
});
Or the some method : 或一些方法 :
results.some(function (result) {
if (result.indicators.length === 0) {
return true;
}
return false;
});
Because Array.forEach
does not return anything. 因为
Array.forEach
不返回任何内容。 You are probably looking for Array.some
which will return a boolean
if any of the items satisfy the confition: 您可能正在寻找
Array.some
,如果任何一项满足配置条件,它将返回一个boolean
:
const invalidResult: boolean = results.some(function (result) {
return result.indicators.length === 0;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.