I came across this question and would like to customize my code in a similar way, except using typescript and a more complicated object.
For example I have the type:
export type ImportationType = {
commercialImportation: boolean
dateToManufacturer: string
barrels: {
assumedAmount: number
sealId: string
uniqueIdentifier?: string
dateShipped?: string
dateImported?: string
}[]
}
And now I would like to extend the barrels
object to have an id: number
type.
So it would be:
export type UpdatedImportationType = {
commercialImportation: boolean
dateToManufacturer: string
barrels: {
id: number
assumedAmount: number
sealId: string
uniqueIdentifier?: string
dateShipped?: string
dateImported?: string
}[]
}
Currently I'm doing the following:
type UpdatedImportationType: Omit<ImportationType, "barrels"> & {
barrels: {
id: number
assumedAmount: number
sealId: string
uniqueIdentifier?: string
dateShipped?: string
dateImported?: string
}[]
}
It seems like there should be a better way of adding the id
field without needing to rewrite the whole type for barrels.
Something like
// Attempt 1
UpdatedImportationType: Omit<ImportationType, "barrels"> & {
barrels: {
...ImportationType["barrels"], // does not work
id: number
}[]
}
//Attempt 2
UpdatedImportationType: ImportationType & {
barrels: {
id: number
}[] // creates two `barrels[]` on the type one with id one with all the other fields.
}
Can someone show me the Typescript "magic" that I don't yet know to solve this?