From 385fa1e3f978b3480c1998c4ee4c34458865b451 Mon Sep 17 00:00:00 2001 From: louwie17 Date: Tue, 12 Jul 2022 11:27:17 -0300 Subject: [PATCH] Add required fields to CrudActions type (#33809) --- packages/js/data/src/crud/types.ts | 36 ++++++++++++------- .../src/product-shipping-classes/types.ts | 7 ++-- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/packages/js/data/src/crud/types.ts b/packages/js/data/src/crud/types.ts index 744df392343..ed3ce19451b 100644 --- a/packages/js/data/src/crud/types.ts +++ b/packages/js/data/src/crud/types.ts @@ -23,24 +23,34 @@ export type ItemQuery = BaseQueryParams & { [ key: string ]: unknown; }; -export type CrudActions< ResourceName, ItemType, MutableProperties > = +type WithRequiredProperty< Type, Key extends keyof Type > = Type & { + [ Property in Key ]-?: Type[ Property ]; +}; + +export type CrudActions< + ResourceName, + ItemType, + MutableProperties, + RequiredFields extends keyof MutableProperties | undefined = undefined +> = MapActions< + { + create: ( query: Partial< ItemType > ) => Item; + update: ( query: Partial< ItemType > ) => Item; + }, + ResourceName, + RequiredFields extends keyof MutableProperties + ? WithRequiredProperty< Partial< MutableProperties >, RequiredFields > + : Partial< MutableProperties >, + Generator< unknown, ItemType > +> & MapActions< { - create: ( query: ItemQuery ) => Item; - update: ( query: ItemQuery ) => Item; + delete: ( id: IdType ) => Item; }, ResourceName, - MutableProperties, + IdType, Generator< unknown, ItemType > - > & - MapActions< - { - delete: ( id: IdType ) => Item; - }, - ResourceName, - IdType, - Generator< unknown, ItemType > - >; + >; export type CrudSelectors< ResourceName, diff --git a/packages/js/data/src/product-shipping-classes/types.ts b/packages/js/data/src/product-shipping-classes/types.ts index 9ac78f8bc9b..79d72e85f26 100644 --- a/packages/js/data/src/product-shipping-classes/types.ts +++ b/packages/js/data/src/product-shipping-classes/types.ts @@ -26,14 +26,13 @@ type Query = BaseQueryParams< keyof ProductShippingClass > & { type ReadOnlyProperties = 'id'; -type MutableProperties = Partial< - Omit< ProductShippingClass, ReadOnlyProperties > ->; +type MutableProperties = Omit< ProductShippingClass, ReadOnlyProperties >; type ProductShippingClassActions = CrudActions< 'ProductShippingClass', ProductShippingClass, - MutableProperties + MutableProperties, + 'name' >; export type ProductShippingClassSelectors = CrudSelectors<