eVolve
eVolve

Reputation: 1446

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

Give I have IInterface1 and I have an array of objects of this type

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

I then have another interface IInterface2

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

I then want to convert all objects in the array of type IInterface1 to IInterface2 the data is the same but I am unable to edit IInterface1. How can this be achieved using typescript?

Upvotes: 1

Views: 756

Answers (2)

Ahmed Daraz
Ahmed Daraz

Reputation: 101

That can be more easier to achieve by just using es6 syntax like:

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

let secondTypeObject = {...firstTypeObject};

Upvotes: 1

DownloadPizza
DownloadPizza

Reputation: 3466

If the data is the same, then where is the problem? Types do not exist at runtime. I do not see why

list1 as IInterface2[]

wouldnt work.

Upvotes: 2

Related Questions