简体   繁体   English

打字稿:检查对象键和值是否适合当前的类属性

[英]Typescript: Check if object keys and values fits to current class properties

I need to check if object keys and values fits to current class properties. 我需要检查对象键和值是否适合当前的类属性。
I have class instance and the changes in separate object then I call Object.assign to modify properties. 我有类实例,并且在单独的对象中进行了更改,然后调用Object.assign来修改属性。 Is there any way how to check if the object key and values are valid for certain class? 有什么方法可以检查对象键和值对于某些类是否有效?

class MyClass {
  icon = 'home'
  size = 12
  color = 'blue'
}

var instance = new MyClass()

var changeProperties: MyClass = { size: 10 }
// throws Property 'icon' is missing in type { size: number; }

Object.assign(instance, changeProperties)

Now got errors for not defined properties (property icon is missing). 现在出现未定义属性的错误(缺少属性图标)。

I have tried var changeProperties: { [string: keyof MyClass]: any } = { size: true } with no success. 我尝试了var changeProperties: { [string: keyof MyClass]: any } = { size: true }没有成功。

Note: I can't change the class itself (eg. make class properties optional). 注意:我无法更改类本身(例如,使类属性为可选)。

I found the answer in lib.es6.d.ts 我在lib.es6.d.ts中找到了答案

/**
 * Make all properties in T optional
 */
type Partial<T> = {
    [P in keyof T]?: T[P];
};

So the answer is: 因此答案是:

class MyClass {
  icon = 'home'
  size = 12
  color = 'blue'
}

var instance = new MyClass()

var changeProperties: Partial<MyClass> = { size: 10 }

Object.assign(instance, changeProperties)

We can always tell TS that we know more, ie 我们总是可以告诉TS我们知道更多,即

// instead of this
var changeProperties: MyClass = { size: 10 }
// we can assure TS that we know more than it..
var changeProperties: MyClass = { size: 10 } as MyClass

That is assertion - a statement for TS - instructing it that the passed object is really what it should be 这就是断言-TS的一条语句-指示它传递的对象确实是应该的对象

Not sure if I understand correctly, but maybe something like that 不知道我是否理解正确,但也许像这样

class MyClass {
   icon = 'home'
   size = 12
   color = 'blue'
}

//just copied from your code, I don't know the purpose of it
var instance = new MyClass()

var changeProperties: MyClass = new MyClass();
var properties = { size: 10, hello: 'test'};

for(const property of Object.getOwnPropertyNames(properties)) {

   if(instance.hasOwnProperty(property) && typeof instance[property] === typeof properties[property]) {

       instance[property] = properties[property];
   }
}

Maybe there is a better solution but that's how I solved something similar. 也许有更好的解决方案,但这就是我解决类似问题的方式。

if you look at instance after the loop you will see that hello has been ignored and is not assigned. 如果在循环后查看instance ,您将看到hello已被忽略并且未分配。 the same happens if you change size in properties to a string. 如果将properties size更改为字符串,也会发生相同的情况。 instance.size will then still be 12. instance.size仍为12。

hope it helps. 希望能帮助到你。

See resources: 查看资源:

It will only work wit IE9+ 仅适用于IE9 +

PS I only tested and used it with simple types like string, numbers etc. PS我只测试并使用了简单的类型,例如字符串,数字等。

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

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