Angular TypeScript错误:键入' void'不能分配给' boolean'

时间:2017-05-29 10:17:00

标签: angular typescript

以下代码有什么问题。 它抱怨对于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;
        }
    }
}

2 个答案:

答案 0 :(得分:3)

forEach method未返回值,signature of the method为:

forEach(callbackfn: (this: void, value: T, index: number, array: T[]) => void): void;

您可能想要使用返回布尔值的every method

results.every(function (result) {
    if (result.indicators.length === 0) {
        return true;
        }
    return false;
});

some method

results.some(function (result) {
    if (result.indicators.length === 0) {
        return true;
    }
    return false;
});

答案 1 :(得分:0)

因为Array.forEach没有返回任何内容。您可能正在寻找Array.some,如果任何项目满足该条件,则会返回monitoring_checklist_id

boolean