volto icon indicating copy to clipboard operation
volto copied to clipboard

@plone/types: Use Generics for contents query to make it easier to type custom content type responses

Open JeffersonBledsoe opened this issue 1 year ago • 4 comments

Is your feature request related to a problem? Please describe.

Currently to get correct typing for a getContent query using @plone/client, we have to do something like the following:

type CustomContentTypeExtraFields = {
  title_number: number;
};
type CustomContentType = Content & CustomContentTypeExtraFields;
const { data }: { data: CustomContentType } = useSuspenseQuery(getContentQuery({ path: flattenToAppURL(pathname) }));

Having to do { data }: { data: CustomContentType } is a little verbose and becomes even more so once you destructure additional keys from the query. You can do something like const customData = data as CustomContentType, but it's still extra code

Proposed solution

Allow getContentQuery to be passed a generic for it's return type. E.g. getContentQuery<CustomContentType>({ path: flattenToAppURL(pathname) })

JeffersonBledsoe avatar Jul 25 '24 15:07 JeffersonBledsoe

@sneridagh what do you think of this proposal?

ichim-david avatar Jul 25 '24 16:07 ichim-david

@JeffersonBledsoe let's talk, because what you are saying it's not true. In the PoCs the typings are gotten right. I can show you anytime. It worked right the last time, and if it's not, we have a regression. This is the magic of using TanStack Query...

sneridagh avatar Jul 30 '24 08:07 sneridagh

@JeffersonBledsoe oh, I see, you are not using declaration merging. As said, this is the only way to go in TS to get all right.

sneridagh avatar Jul 30 '24 08:07 sneridagh

@JeffersonBledsoe ok I gave it a second read, sorry but these days are frentic. Let's talk at the meeting.

sneridagh avatar Jul 30 '24 08:07 sneridagh

Hi There! 👋

We haven't seen any activity on this issue in a while :sleeping:, and we want to make sure that it's still relevant.

If updating to the latest version of Volto doesn't help, please let us know by:

  • adding a comment about what needs to be done next 💬
  • updating its status and other labels 🏷️

Otherwise close this issue. 🧹

github-actions[bot] avatar Mar 27 '25 00:03 github-actions[bot]