繁体   English   中英

当属性名称相同时,如何使用 typescript 将 map 和 object 转换为另一个接口类型

[英]How to map an object to another interface type when the property names are the same using typescript

给我有IInterface1并且我有一个这种类型的对象数组

export interface IInterface1 {
  id: string;
  status: string;
  otherStatus: string;
  title: string;
  person: string;
  needed: IThisOtherInterface[];
}

然后我有另一个接口IInterface2

export interface IInterface2{
      id: string;
      status: string;
      otherStatus: string;
      title: string;
      person: string;
      needed: IThisOtherInterface2[];
}

然后我想将IInterface1类型的数组中的所有对象转换为IInterface2数据是相同的,但我无法编辑IInterface1 使用 typescript 如何实现这一点?

如果数据相同,那么问题出在哪里? 类型在运行时不存在。 我不明白为什么

list1 as IInterface2[]

不会工作。

这可以通过使用 es6 语法更容易实现,例如:

let firstTypeObject = {
  id: 1,
  status: "Active",
  title: "Mr"
};

let secondTypeObject = {...firstTypeObject};

暂无
暂无

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

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