首页 > 解决方案 > 类型“数字”不可分配给类型“布尔”

问题描述

代码:- 1. 部分:Departments[];

let sectionCount:number;
    sectionCount = +this.sections.find(x =>x.sections).sections;

错误。

 error TS2345: Argument of type '(this: void, x: Departments) => number' is not assignable to parameter of type '(value: Departments, index: number, obj: Departments[]) => boolean'.
  Type 'number' is not assignable to type 'boolean'.

标签: angular

解决方案


如果您从错误中解释 Type 'number' is not assignable to type 'boolean'它与sectionCount.

正如错误所说, find() 方法必须返回一个布尔值而不是一个数字 -

尝试类似的东西 -

sectionCount = this.sections.find(x => {
  return x.sections > 0;
}).sections;

推荐阅读