Created
February 8, 2024 05:56
-
-
Save devmnj/deb514b5cf93da6540b4ea1aea0d278a to your computer and use it in GitHub Desktop.
Typescript array map to a different type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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