From 650e0cd42f21b0077962734d3e7d38f67ef0a014 Mon Sep 17 00:00:00 2001 From: Angelo Ashmore Date: Tue, 9 Apr 2024 16:38:04 -1000 Subject: [PATCH] fix: value type --- src/types/value/sharedSlice.ts | 7 +++-- src/types/value/sharedSliceVariation.ts | 37 +++++++++++++++---------- test/types/fields-sharedSlice.types.ts | 5 ++++ 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/types/value/sharedSlice.ts b/src/types/value/sharedSlice.ts index c3215ee0..39df28e7 100644 --- a/src/types/value/sharedSlice.ts +++ b/src/types/value/sharedSlice.ts @@ -1,4 +1,7 @@ -import type { SharedSliceVariation } from "./sharedSliceVariation"; +import type { + SharedSliceVariation, + SharedSliceVariationBase, +} from "./sharedSliceVariation"; /** * A shared Slice. @@ -7,7 +10,7 @@ import type { SharedSliceVariation } from "./sharedSliceVariation"; */ export type SharedSlice< SliceType = string, - Variations extends SharedSliceVariation = SharedSliceVariation, + Variations extends SharedSliceVariationBase = SharedSliceVariation, > = { slice_type: SliceType; slice_label: null; diff --git a/src/types/value/sharedSliceVariation.ts b/src/types/value/sharedSliceVariation.ts index 8ecde3b4..59cf5150 100644 --- a/src/types/value/sharedSliceVariation.ts +++ b/src/types/value/sharedSliceVariation.ts @@ -1,11 +1,18 @@ import type { AnyRegularField, SliceField } from "./types"; +export type SharedSliceVariationBase = { + variation: Variation; + version: string; +}; + /** * A shared Slice variation. Content is in the `primary` and `items` properties. */ export type SharedSliceVariationWithPrimaryAndItems< - Variation = string, - PrimaryFields extends Record = Record< + Variation extends string = string, + // SliceField is used to support a top-level SharedSliceVariation type. + // It must support `AnyRegularField` and `SliceField`. + PrimaryFields extends Record = Record< string, AnyRegularField >, @@ -13,30 +20,32 @@ export type SharedSliceVariationWithPrimaryAndItems< string, AnyRegularField >, -> = Omit, "data">; +> = SharedSliceVariationBase & { + primary: PrimaryFields; + items: ItemsFields[]; +}; /** * A shared Slice variation. Content is in the `data` property. */ export type SharedSliceVariationWithData< - Variation = string, + Variation extends string = string, Fields extends Record = Record, -> = Omit, "primary" | "items">; +> = SharedSliceVariationBase & { + variation: Variation; + version: string; + data: Fields; +}; /** * A shared Slice variation. */ -export interface SharedSliceVariation< - Variation = string, +export type SharedSliceVariation< + Variation extends string = string, Fields extends Record = Record, ItemsFields extends Record = Record< string, AnyRegularField >, -> { - variation: Variation; - version: string; - data: Fields; - primary: Fields; - items: ItemsFields[]; -} +> = SharedSliceVariationWithData & + SharedSliceVariationWithPrimaryAndItems; diff --git a/test/types/fields-sharedSlice.types.ts b/test/types/fields-sharedSlice.types.ts index 723fbb5f..e68b8d66 100644 --- a/test/types/fields-sharedSlice.types.ts +++ b/test/types/fields-sharedSlice.types.ts @@ -24,6 +24,7 @@ expectType({ slice_label: null, variation: "string", version: "string", + data: {}, primary: {}, items: [], }); @@ -37,6 +38,7 @@ expectType>({ slice_label: null, variation: "string", version: "string", + data: {}, primary: {}, items: [], }); @@ -47,6 +49,7 @@ expectType>({ slice_label: null, variation: "string", version: "string", + data: {}, primary: {}, items: [], }); @@ -60,6 +63,7 @@ expectType>>({ slice_label: null, variation: "foo", version: "string", + data: {}, primary: {}, items: [], }); @@ -70,6 +74,7 @@ expectType>>({ // @ts-expect-error - Variation type must match the given type. variation: "string", version: "string", + data: {}, primary: {}, items: [], });