Create a typescript generic for Contentful SDK methods - Stack Overflow

I have following problem. I'm trying to write a generic type to handle getting data from Contentfu

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

相关推荐

  • Create a typescript generic for Contentful SDK methods - Stack Overflow

    I have following problem. I'm trying to write a generic type to handle getting data from Contentfu

    11小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信