[英]Typescript: How to derive one type from another using a map within the same interface
[英]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.