【发布时间】:2019-06-17 09:30:25
【问题描述】:
我在 typescript 中有一个 any 对象,我需要对其进行整形,使其对给定接口有效。
我需要一种方法来创建新对象,以清除不属于类定义的属性并添加缺失的属性。
代码示例可以是:
interface ImyInterface {
a: string;
b: string;
c?:string;
};
let myObject = {
a: "myString",
d: "other value"
};
我的问题是:有没有办法转换/过滤myObject,使其符合接口ImyInterface 的定义,并转换为此
console.log (JSON.stringify(objectA));
> {a: 'myString', b: null}
【问题讨论】:
-
请出示您已有的代码并评论您正在努力解决的部分。
标签: javascript typescript interface graphql