Skip to content

Instantly share code, notes, and snippets.

@devmnj
Created February 8, 2024 05:56
Show Gist options
  • Save devmnj/deb514b5cf93da6540b4ea1aea0d278a to your computer and use it in GitHub Desktop.
Save devmnj/deb514b5cf93da6540b4ea1aea0d278a to your computer and use it in GitHub Desktop.
Typescript array map to a different type
//target type
export type LatestInvoice = {
id: string;
name: string;
image_url: string;
email: string;
amount: string;
};
//source type
export type LatestInvoiceType={
id:number;
amount: number;
customer:{
name:string;
email:string;
image_url:string;
}
}
const latestInvoices = invoices.map(invoice => {
return{
id:invoice.id.toString(),
amount:invoice.amount.toString(),
name:invoice.customer?.name,
email:invoice.customer?.email,
image_url:invoice.customer?.image_url
};
}) as LatestInvoice[] ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment