From 7cff55e582458964353ac650346cbb0471a500ba Mon Sep 17 00:00:00 2001 From: Lucie <25330882+lihbr@users.noreply.github.com> Date: Thu, 16 Jan 2025 15:15:50 +0100 Subject: [PATCH] fix: allow generics with link variant (#374) Co-authored-by: lihbr --- src/types/value/link.ts | 35 ++++++++------ src/types/value/linkToMedia.ts | 19 +++++--- test/types/fields-link.types.ts | 82 +++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 19 deletions(-) diff --git a/src/types/value/link.ts b/src/types/value/link.ts index 41a30fb8..ff13a612 100644 --- a/src/types/value/link.ts +++ b/src/types/value/link.ts @@ -23,6 +23,7 @@ export const LinkType = { * @typeParam DataInterface - Data fields for the document (filled via the * `fetchLinks` or `graphQuery` query parameter). * @typeParam State - State of the field which determines its shape. + * @typeParam Variant - Variants of the link. */ export type LinkField< TypeEnum = string, @@ -31,9 +32,10 @@ export type LinkField< | Record | unknown = unknown, State extends FieldState = FieldState, + Variant = string, > = State extends "empty" - ? EmptyLinkField - : FilledLinkField + ? EmptyLinkField + : FilledLinkField /** * A link field that is filled. @@ -42,6 +44,7 @@ export type LinkField< * @typeParam LangEnum - Language API ID of the document. * @typeParam DataInterface - Data fields for the document (filled via the * `fetchLinks` or `graphQuery` query parameter). + * @typeParam Variant - Variants of the link. */ export type FilledLinkField< TypeEnum = string, @@ -49,45 +52,51 @@ export type FilledLinkField< DataInterface extends | Record | unknown = unknown, -> = ( - | FilledContentRelationshipField - | FilledLinkToMediaField - | FilledLinkToWebField -) & - OptionalLinkProperties + Variant = string, +> = + | (FilledContentRelationshipField & + OptionalLinkProperties) + | FilledLinkToMediaField + | FilledLinkToWebField /** * A link field that is not filled. * * @typeParam _Unused - THIS PARAMETER IS NOT USED. If you are passing a type, * **please remove it**. + * @typeParam Variant - Variants of the link. */ export type EmptyLinkField< _Unused extends (typeof LinkType)[keyof typeof LinkType] = typeof LinkType.Any, + Variant = string, > = { link_type: "Any" -} & OptionalLinkProperties +} & OptionalLinkProperties /** * A link field pointing to a relative or absolute URL. + * + * @typeParam Variant - Variants of the link. */ -export type FilledLinkToWebField = { +export type FilledLinkToWebField = { link_type: "Web" url: string target?: string -} & OptionalLinkProperties +} & OptionalLinkProperties /** * Optional properties available to link fields. It is used to augment existing * link-like fields (like content relationship fields) with field-specific * properties. * + * @typeParam Variant - Variants of the link. + * * @internal */ // Remember to update the `getOptionalLinkProperties()` function when updating // this type. The function should check for every property. -export type OptionalLinkProperties = { +export type OptionalLinkProperties = { text?: string - variant?: string + variant?: Variant } diff --git a/src/types/value/linkToMedia.ts b/src/types/value/linkToMedia.ts index 0ddd5f7d..0b65c961 100644 --- a/src/types/value/linkToMedia.ts +++ b/src/types/value/linkToMedia.ts @@ -6,18 +6,25 @@ import type { OptionalLinkProperties } from "./link" * A link field that points to media. * * @typeParam State - State of the field which determines its shape. + * @typeParam Variant - Variants of the link. */ -export type LinkToMediaField = - State extends "empty" ? EmptyLinkToMediaField : FilledLinkToMediaField +export type LinkToMediaField< + State extends FieldState = FieldState, + Variant = string, +> = State extends "empty" + ? EmptyLinkToMediaField + : FilledLinkToMediaField -type EmptyLinkToMediaField = { +type EmptyLinkToMediaField = { link_type: "Any" -} & OptionalLinkProperties +} & OptionalLinkProperties /** * A link that points to media. + * + * @typeParam Variant - Variants of the link. */ -export type FilledLinkToMediaField = { +export type FilledLinkToMediaField = { id: string link_type: "Media" name: string @@ -26,4 +33,4 @@ export type FilledLinkToMediaField = { size: string height?: string | null width?: string | null -} & OptionalLinkProperties +} & OptionalLinkProperties diff --git a/test/types/fields-link.types.ts b/test/types/fields-link.types.ts index b8827548..e5618839 100644 --- a/test/types/fields-link.types.ts +++ b/test/types/fields-link.types.ts @@ -160,6 +160,88 @@ expectType>({ link_type: prismic.LinkType.Any, variant: "string", }) +expectType>({ + link_type: prismic.LinkType.Any, + variant: "foo", +}) +expectType>({ + link_type: prismic.LinkType.Any, + // @ts-expect-error - Variant must match the given enum. + variant: "string", +}) + +/** + * Filled state with explicit variant. + */ +expectType>({ + link_type: prismic.LinkType.Web, + url: "string", + target: "string", + text: "string", + variant: "foo", +}) +expectType>({ + link_type: prismic.LinkType.Document, + id: "string", + uid: "string", + type: "string", + tags: ["string"], + lang: "string", + url: "string", + slug: "string", + isBroken: true, + data: undefined, + text: "string", + variant: "foo", +}) +expectType>({ + link_type: prismic.LinkType.Media, + id: "string", + name: "string", + kind: "string", + url: "string", + size: "string", + height: "string", + width: "string", + text: "string", + variant: "foo", +}) +expectType>({ + link_type: prismic.LinkType.Web, + url: "string", + target: "string", + text: "string", + // @ts-expect-error - Variant must match the given enum. + variant: "string", +}) +expectType>({ + link_type: prismic.LinkType.Document, + id: "string", + uid: "string", + type: "string", + tags: ["string"], + lang: "string", + url: "string", + slug: "string", + isBroken: true, + data: undefined, + text: "string", + // @ts-expect-error - Variant must match the given enum. + variant: "string", +}) +expectType>({ + link_type: prismic.LinkType.Media, + id: "string", + name: "string", + kind: "string", + url: "string", + size: "string", + height: "string", + width: "string", + text: "string", + // @ts-expect-error - Variant must match the given enum. + variant: "string", +}) /** * Supports custom document type for document links.