Partial
partial of a type: accept any subset
Make properties of an existing type optional, creating a new type. Conforming objects may contain any subset of the original properties:
interface SearchConfig {
query: string
inStock: boolean
minPrice: number
maxPrice: number
}
const searchConfig: Partial<SearchConfig> = {
query: "RTX 5060",
inStock: true,
}
example: typing merge update data
When updating an existing record with a merge strategy, we only provide the properties to be changed, that is, a subset of the original record:
type ProductUpdate = Partial<Product>
(advanced) Note: if the update should not affect some properties because they are immutable, we either:
- white-list the mutable properties that can be included in the update object with Pick:
type ProductUpdate = Partial<Pick<Product, "price" | "description">>
- exclude the immutable properties with Omit:
type ProductUpdate = Partial<Omit<Product, "id" | "slug">>