【问题标题】:argument of type 'any' is not assignable to parameter of type 'never' typescript solution“任何”类型的参数不能分配给“从不”类型的参数打字稿解决方案
【发布时间】:2020-12-28 08:28:05
【问题描述】:

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();
    });
  }

【问题讨论】:

标签: javascript arrays angular typescript


【解决方案1】:

问题

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

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

临时修复

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

this.userObj.assigned_to.push(newUser as never);

【讨论】:

  • 我在这么多地方上 :( 'never' 类型的属性不存在。
【解决方案2】:

改变

assigned_to: [];

assigned_to = [];

【讨论】:

    猜你喜欢
    • 2021-04-09
    • 2019-06-16
    • 2021-10-18
    • 2018-08-05
    • 2021-07-23
    • 2016-09-26
    • 2021-12-10
    • 2021-10-18
    • 2019-09-23
    相关资源
    最近更新 更多