“any”类型的参数不能分配给“never”类型的参数

时间:2020-12-28 08:28:05

标签: javascript arrays angular typescript

我在此处收到 newUser this.userObj.assigned_to.push(newUser); as argument of type 'any' is not assignable to parameter of type 'never' typescript solution 的错误

如何声明 newUser 以消除错误?

感谢您的帮助。

addSubData(user: any) {
    let i: any;
    i = document.querySelectorAll('input[type="checkbox"]:checked');
    for(var checked of i) {
      const newUser = Object.assign({}, user)
      newUser.dl = checked.value;
      newUser.sub_impact = "Applicable";
      newUser.co_score = "Added";
      this.userObj.assigned_to.push(newUser);
    }
    this.commonService.updateUser(this.userObj).subscribe(()=> {
      this.getLatestUser();
    });
  }

2 个答案:

答案 0 :(得分:1)

改变

assigned_to: [];

assigned_to = [];

答案 1 :(得分:1)

问题

您的代码似乎错过了定义足够多的类型,因此很难为您提供最佳修复方法(显然最好的方法是为您定义的内容(例如类成员)填写类型)。

无论如何,这里的错误是 assigned_to 的数组必须作为每个成员类型传递 never 类型,但现在您传递的是 any 类型。

临时修复

您可以将 newUser 类型的 any 转换为 never 类型可以解决问题:

this.userObj.assigned_to.push(newUser as never);
相关问题