Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
JeffersonBledsoe opened this issue Jul 25, 2024 · 4 comments

Comments

@JeffersonBledsoe
Copy link
Member

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) })

@ichim-david
Copy link
Sponsor Member

@sneridagh what do you think of this proposal?

@sneridagh
Copy link
Member

@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
Copy link
Member

@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
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants