简体   繁体   English

仅动态复制 Typescript 中 2 个相同类型的 object 之间的一些属性

[英]Dynamic copy only some properties between 2 same typed object in Typescript

Hello All I'm getting tsc compilation error when try to merge 2 objects dynamically: I try to merge only some properties of the object don't want to copy all of them.大家好,我在尝试动态合并 2 个对象时遇到 tsc 编译错误:我尝试仅合并 object 的某些属性,不想复制所有这些属性。 The example below is a method in a typescript Class that implements the same interface of the parameter received, "iUser" , so "this" is the same type object of the received parameter( "userSave" )下面的例子是typescript Class中的一个方法,它实现了与接收参数“iUser”相同的接口,因此“this”与接收参数的类型相同( “userSave”

expample:例子:

 updatefromUser(userSave: iUser) {
            let propToValidate: (keyof iUser)[]
            propToValidate = ["Country", "City", "State", "Zip", "VATnr", "Language"]
            let updateData: boolean = false;
            propToValidate.forEach(ele => {
                if (this[ele] != userSave[ele]) {
                    this[ele] =  userSave[ele]
                    updateData = true
                }
            });
            return updateData
        }

the compilation error says:编译错误说:

error TS2322: Type 'string |错误 TS2322:类型“字符串 | string[] |字符串[] | Date |日期 | iAuthUserId[] | iAuthUserId[] | iAuthUser[] | iAuthUser[] | null' is not assignable to type 'string & iAuthUserId[] & iAuthUser[] & Date & string[]'. null' 不可分配给类型 'string & iAuthUserId[] & iAuthUser[] & Date & string[]'。 Type 'null' is not assignable to type 'string & iAuthUserId[] & iAuthUser[] & Date & string[]'.类型 'null' 不可分配给类型 'string & iAuthUserId[] & iAuthUser[] & Date & string[]'。 Type 'null' is not assignable to type 'string'.类型“null”不能分配给类型“string”。 112 this[ele] = userSave[ele] ~~~~~~~~~ 112 这个[ele] = userSave[ele] ~~~~~~~~~

Anyone knows how to do this dynamically?任何人都知道如何动态地做到这一点?

Maybe there are some properties in a class definition that are more strict than those in iUser interface?也许 class 定义中的某些属性比 iUser 界面中的属性更严格? Because your error tells something similar to this.因为您的错误告诉了与此类似的内容。 For example, consider the code:例如,考虑以下代码:

interface iUser {
  id: string,
  name: string|null
}

class User implements iUser {
  id: string;
  name: string;

  update(userSave: iUser) {
    let propToValidate: (keyof iUser)[]
    propToValidate = ["id", "name"]
    let updateData = false;
    propToValidate.forEach(ele => {
      if (this[ele] != userSave[ele] && typeof this[ele] === typeof userSave[ele]) {
          this[ele]=  userSave[ele]
          updateData = true
      }
    });
    return updateData
  }
}

iUser interface allows the name to be null, but the class does not. iUser 界面允许名称为 null,但 class 不允许。 There will be the error similar to yours, unfortunately it does not tell us what exactly property of iUser is not compatible with User class.会有类似你的错误,不幸的是它没有告诉我们 iUser 的确切属性与用户 class 不兼容。

Possible solution would be to make all properties the same, or check those properties that are different in iUser and in the class, something like this:可能的解决方案是使所有属性相同,或者检查 iUser 和 class 中不同的属性,如下所示:

...
propToValidate.forEach(ele => {
  if (this[ele] != userSave[ele] && typeof this[ele] === typeof userSave[ele]) {
      if (ele === 'name') {
        this[ele] =  userSave[ele] ?? ''
      }
      else {
        this[ele]=  userSave[ele]
      }
      updateData = true
  }
});
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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