ARTICLE AD BOX
What is the best practice for creating the PerishableIngredient object? Also should I try to get the compiler to hint at the required fields, allow omission of optional fields, and not allow properties that are neither optional or required?
Fore example I tried to create a function that calls createDomainObject\<PerishableIngredient\>('perishable-ingredient', {}) and for the compiler to enforce what I said above on the {} in the second argument.
I wanted to use a registry for all of my concrete types (perishable ingredient, nonperishable ingredient, equipment...) but I'm struggling with a lot of type errors.
The types IsPerishable and IsIngredient contain a mix of optional and required types.
export interface NamedCoreModel< BrandName extends string = string, TKind extends string = string > extends CoreModel<BrandName, TKind> { name: string; } export interface InventoryItemBase<TKind extends InventoryItemKind["kind"]> extends NamedCoreModel<InventoryItemId, TKind> { dateAdded: Date; notes?: string; } export type PerishableIngredient = InventoryItemBase<"perishable-ingredient"> & IsPerishable & IsIngredient;I wanted to use a registry for all of my concrete types (perishable ingredient, nonperishable ingredient, equipment...) but I'm struggling with a lot of type errors when i put everything together. I'm thinking it might not be worth it. Would like a second opinion before i make a factory for each base type instead of the registry.
export const DOMAIN_REGISTRY: { [K in keyof DomainTypes]: RegistryEntry<DomainTypes[K], FactoryContext>; } = { "inventory-item": { fields: ["name", "kind"] as const, defaults: { dateAdded: (ctx) => new Date(ctx.now), notes: undefined, }, variants: INVENTORY_REGISTRY, }, //... export const INVENTORY_REGISTRY: { [K in keyof InventoryVariantMap]: RegistryEntry< InventoryVariantMap[K], FactoryContext >; } = { "perishable-ingredient": { fields: ["quantity", "masterIngedientId"] as const, defaults: { kind: "perishable-ingredient", openDate: undefined, sellByDate: undefined, category: "ingredient", bestByDate: (ctx) => new Date(ctx.now + 3 * 24 * 60 * 60 * 1000), }, //...