I have following problem. I'm trying to write a generic type to handle getting data from Contentful.
export type EntriesWithSlug = TypePageSkeleton | TypeBlogPostSkeleton;
export const getEntryBySlug =
<T extends EntriesWithSlug>(contentType: T["contentTypeId"]) =>
async (slug: string) => {
const entries = await client.getEntries<T>({
content_type: contentType,
"fields.slug": slug,
});
const entry = entries.items[0];
return entry;
};
export const getPageBySlug = getEntryBySlug<TypePageSkeleton>("page");
export const getBlogPostBySlug = getEntryBySlug<TypeBlogPostSkeleton>("blogPost");
The types are working perfectly, when I use method getPageBySlug
the response is correctly typed etc. The only issue I have is with parameters in getEntries
method. I'm using a generic there, and it has a constrain to be EntryWithSlug
. Because of that, I should be able to add different conditions, like "field.slug" or whatever other field is there. But instead I have an error: Object literal may only specify known properties, and '"fields.slug"' does not exist in type 'EntriesQueries<T, undefined>'
I know what that means, but I don't see any solution to it. If instead of 'T' I put whatever other type, the conditions are typed correctly, and I can use them like: "fields.description", "fields.hero" whatever field the type has. But then the types for response are not valid. Any idea how I can fix it?
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742400424a4436804.html
评论列表(0条)