Skip to content

Commit

Permalink
fix: value type
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed Apr 10, 2024
1 parent f65f07b commit 650e0cd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
7 changes: 5 additions & 2 deletions src/types/value/sharedSlice.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { SharedSliceVariation } from "./sharedSliceVariation";
import type {
SharedSliceVariation,
SharedSliceVariationBase,
} from "./sharedSliceVariation";

/**
* A shared Slice.
Expand All @@ -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;
Expand Down
37 changes: 23 additions & 14 deletions src/types/value/sharedSliceVariation.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,51 @@
import type { AnyRegularField, SliceField } from "./types";

export type SharedSliceVariationBase<Variation extends string = string> = {
variation: Variation;
version: string;
};

/**
* A shared Slice variation. Content is in the `primary` and `items` properties.
*/
export type SharedSliceVariationWithPrimaryAndItems<
Variation = string,
PrimaryFields extends Record<string, AnyRegularField> = Record<
Variation extends string = string,
// SliceField is used to support a top-level SharedSliceVariation type.
// It must support `AnyRegularField` and `SliceField`.
PrimaryFields extends Record<string, SliceField> = Record<
string,
AnyRegularField
>,
ItemsFields extends Record<string, AnyRegularField> = Record<
string,
AnyRegularField
>,
> = Omit<SharedSliceVariation<Variation, PrimaryFields, ItemsFields>, "data">;
> = SharedSliceVariationBase<Variation> & {
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<string, SliceField> = Record<string, SliceField>,
> = Omit<SharedSliceVariation<Variation, Fields>, "primary" | "items">;
> = SharedSliceVariationBase<Variation> & {
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<string, SliceField> = Record<string, SliceField>,
ItemsFields extends Record<string, AnyRegularField> = Record<
string,
AnyRegularField
>,
> {
variation: Variation;
version: string;
data: Fields;
primary: Fields;
items: ItemsFields[];
}
> = SharedSliceVariationWithData<Variation, Fields> &
SharedSliceVariationWithPrimaryAndItems<Variation, Fields, ItemsFields>;
5 changes: 5 additions & 0 deletions test/types/fields-sharedSlice.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ expectType<prismic.SharedSlice>({
slice_label: null,
variation: "string",
version: "string",
data: {},
primary: {},
items: [],
});
Expand All @@ -37,6 +38,7 @@ expectType<prismic.SharedSlice<"foo">>({
slice_label: null,
variation: "string",
version: "string",
data: {},
primary: {},
items: [],
});
Expand All @@ -47,6 +49,7 @@ expectType<prismic.SharedSlice<"foo">>({
slice_label: null,
variation: "string",
version: "string",
data: {},
primary: {},
items: [],
});
Expand All @@ -60,6 +63,7 @@ expectType<prismic.SharedSlice<string, prismic.SharedSliceVariation<"foo">>>({
slice_label: null,
variation: "foo",
version: "string",
data: {},
primary: {},
items: [],
});
Expand All @@ -70,6 +74,7 @@ expectType<prismic.SharedSlice<string, prismic.SharedSliceVariation<"foo">>>({
// @ts-expect-error - Variation type must match the given type.
variation: "string",
version: "string",
data: {},
primary: {},
items: [],
});

0 comments on commit 650e0cd

Please sign in to comment.