You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
Content from a content relationship field's document can be included in a query using the fetchLinks or graphQuery options. Typing those fields with TypeScript is difficult, however, requiring type assertions (as) or manual type parameters (getByUID<...>).
Describe the solution you'd like
A helper function could be provided that checks if a field is included in the query and automatically types the property. We can accomplish this using @prismicio/client's Content namespace.
Something like the following isFilledRelatedData could be used:
import{Content,FilledContentRelationshipField,LinkField,isFilled,}from"@prismicio/client";typeDocumentData<TDocumentTypeextendsContent.AllDocumentTypes["type"]>=Extract<Content.AllDocumentTypes,{type: TDocumentType}>["data"];exportfunctionisFilledRelatedData<TDocumentTypeextendsContent.AllDocumentTypes["type"],TFieldIDextendskeyofDocumentData<TDocumentType>,>(linkField: LinkField,documentType: TDocumentType,fieldID: TFieldID,): linkField is FilledContentRelationshipField&{data: {[PinkeyofDocumentData<TDocumentType>asPextendsTFieldID
? P
: never]: DocumentData<TDocumentType>[P];};}{return(isFilled.contentRelationship(linkField)&&linkField.type===documentType&&typeoflinkField.data==="object"&&linkField.data!==null&&fieldIDinlinkField.data);}
isFilledRelatedData()'s name can be misleading since it doesn't actually check if the field is filled. Instead, it checks that the linked field was queried via fetchLinks/graphQuery.
We would probably refocus the helper into something like hasRelationshipField(), which would still require you to check that the field was filled with isFilled().
Is your feature request related to a problem? Please describe.
Content from a content relationship field's document can be included in a query using the
fetchLinks
orgraphQuery
options. Typing those fields with TypeScript is difficult, however, requiring type assertions (as
) or manual type parameters (getByUID<...>
).Describe the solution you'd like
A helper function could be provided that checks if a field is included in the query and automatically types the property. We can accomplish this using
@prismicio/client
'sContent
namespace.Something like the following
isFilledRelatedData
could be used:isFilledRelatedData()
's name can be misleading since it doesn't actually check if the field is filled. Instead, it checks that the linked field was queried viafetchLinks
/graphQuery
.We would probably refocus the helper into something like
hasRelationshipField()
, which would still require you to check that the field was filled withisFilled()
.Describe alternatives you've considered
Project-specific runtime checkers can be written using
isFilled
, but it is cumbersome to write and maintain.See https://community.prismic.io/t/types-for-content-relationship-in-a-slice/12067/2 for an example.
Additional context
The text was updated successfully, but these errors were encountered: