简体   繁体   English

使用for..in的对象的Typescript复制属性

[英]Typescript copy properties of an object using for..in

I'm trying to copy the properties of an object using for..in but I got the error: 我正在尝试使用for..in复制对象的属性,但出现错误:

Type 'Greeter[Extract]' is not assignable to type 'this[Extract]'. 类型“ Greeter [Extract]”不能分配给类型“ this [Extract]”。

Any ideas how to solve this? 任何想法如何解决这个问题?

class Greeter {
a: string;
b: string;
c: string;
// etc

constructor(cloned: Greeter) {

    for (const i in this) {
        if (cloned.hasOwnProperty(i)) {
            this[i] = cloned[i];
        }
    }
}

Here is the sample in the typescript playground. 是打字稿游乐场中的示例。

Thanks! 谢谢!

The problem is that the type of this is not Greeter it's the polymorphic this type . 问题是,类型this是不是Greeter它是多态的this类型 An unfortunate consequence is that the i in your for loop i typed as keyof this while Greeting can be indexed using a keyof Greeting . 一个不幸后果是, i在你的for循环我输入的keyof thisGreeting可以使用索引keyof Greeting These may seem like the same thing, but if you consider that Greeting can be derived, keyof this could potentially contain a lot more members. 这些看起来似乎是一回事,但是如果您认为可以派生Greeting ,则其中的keyof this可能包含更多的成员。 A similar discussion applies to the value of the indexing operation. 类似的讨论适用于索引操作的值。

The compiler is not wrong, this may have more keys than Greeter so this is not 100% safe. 编译器没有错, this可能比Greeter具有更多的密钥,因此不是100%安全的。

The simplest solution is to use a type assertion to change the type of this : 最简单的解决方法是使用一种类型的断言改变的类型this

class Greeter {
    a: string;
    b: string;
    c: string;
    // etc

    constructor(cloned: Greeter) {
        for (const i in this as Greeter) {
            if (cloned.hasOwnProperty(i)) {
                this[i] = cloned[i]
            }
        }

    }
}

Or you can iterate over the cloned object: 或者,您可以遍历cloned对象:

class Greeter {
    a: string;
    b: string;
    c: string;
    // etc

    constructor(cloned: Greeter) {
        for (const i in cloned) {
            if (cloned.hasOwnProperty(i)) {
                this[i] = cloned[i]
            }
        }

    }
}

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

相关问题 在TypeScript中使用解构/扩展来复制具有重命名属性的对象 - Using destructuring/spreading to copy object with renamed properties in TypeScript 使用 .map() 复制属性 typescript - Copy properties using .map() typescript 使用 Typescript 时如何避免触发 eslint 的 for..in - How to avoid triggering eslint's for..in when using Typescript TypeScript 复制对象的属性创建一个新的 object - TypeScript copy object's properties to create a new object Typescript创建具有所有键属性但类型不同的Object副本 - Typescript create Object copy with all key properties but different type 在 TypeScript 中将对象属性复制到另一个,导致错误 TS2339 - Copy a object properties to another in TypeScript, cause error TS2339 typescript如何仅将属性和方法从接口复制到新对象? - typescript how to copy only properties and methods from interface to a new object? 如何将属性从一个 object 复制到另一个 typescript - How to copy properties from one object to another in typescript 仅动态复制 Typescript 中 2 个相同类型的 object 之间的一些属性 - Dynamic copy only some properties between 2 same typed object in Typescript 使用 TypeScript 动态返回对象属性类型 - Dynamic return type of object properties using TypeScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM