简体   繁体   中英

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.

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(callbackfn: (this: void, value: T, index: number, array: T[]) => void): void;

You probably want to use the every method which returns a boolean:

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. You are probably looking for Array.some which will return a boolean if any of the items satisfy the confition:

const invalidResult: boolean = results.some(function (result) {
    return result.indicators.length === 0;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM