【发布时间】:2020-05-02 08:54:40
【问题描述】:
我想从另一个对象创建一个ExampleInterface 对象,但只保留ExampleInterface 包含的那些属性。
是否可以不手动复制每个键?
export interface ExampleInterface {
property1: string;
property2: string;
}
然后
const exampleObject: ExampleInterface = anotherObjectThatHasMoreProperties;
提前谢谢你。
【问题讨论】:
-
TypeScript 类型不会生成运行时类型,因此开箱即用您无法执行此操作。为什么需要在运行时剥离额外的属性?可能最简单的解决方案是创建一个手动复制所需属性的函数。还有libraries designed to bring runtime type features to TS,他们可能会有所帮助。
标签: typescript