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

Types issue #2017

Open
damian-turek opened this issue Sep 19, 2023 · 4 comments
Open

Types issue #2017

damian-turek opened this issue Sep 19, 2023 · 4 comments

Comments

@damian-turek
Copy link

damian-turek commented Sep 19, 2023

Hi, I have a problem adding types when I fetch photos from contentful. In the src section I type "https:'+e.fields.portrait.fields.file?.url", but it shows me an error in "fields" that reads: Property fields does not exist on type never.

The second problem is with the order setting. While querying "content type" and "order" in the order section an error pops up for me: (OrderFilterPaths<EntrySys, 'sys'> | 'sys.contentType.sys.id' | '-sys.contentType.sys.id')[]

I can't solve these problems. Could someone help me with this?

Translated with www.DeepL.com/Translator (free version)

Screenshot 2023-09-19 at 10 38 30 Screenshot 2023-09-19 at 10 38 53
@marcolink
Copy link
Member

Hi @damian-turek 👋
Can you please share a more complete example?
Would be interesting to see the type passed in as the generic for getEntries

@samzmann
Copy link

I'm getting the same issue:

import { contentfulClient } from '@/contentful/api/contentfulClient'
import { TypeProjectSkeleton } from '@/contentful/types'

export const getAllProjects = async (locale: string) => {
  const response =
    await contentfulClient.withoutUnresolvableLinks.getEntries<TypeProjectSkeleton>(
      {
        content_type: 'project',
        locale,
        order: '-fields.startYear', // TS2322: Type  string  is not assignable to type (OrderFilterPaths<EntrySys, 'sys'> | 'sys.contentType.sys.id' | '-sys.contentType.sys.id')[]
      },
    )

  return response.items
}

My types:

import type { ChainModifiers, Entry, EntryFieldTypes, EntrySkeletonType, LocaleCode } from "contentful";

export interface TypeProjectFields {
    title: EntryFieldTypes.Symbol;
    shortTitle?: EntryFieldTypes.Symbol;
    subtitle?: EntryFieldTypes.Symbol;
    slug: EntryFieldTypes.Symbol;
    body: EntryFieldTypes.RichText;
    startYear?: EntryFieldTypes.Integer;
    endYear?: EntryFieldTypes.Integer;
    images?: EntryFieldTypes.Array<EntryFieldTypes.AssetLink>;
}

export type TypeProjectSkeleton = EntrySkeletonType<TypeProjectFields, "project">;
export type TypeProject<Modifiers extends ChainModifiers, Locales extends LocaleCode> = Entry<TypeProjectSkeleton, Modifiers, Locales>;

I'm generating the types with cf-content-types-generator@2.12.9 (latest as of today): https://github.com/contentful-labs/cf-content-types-generator/

@mcknight89
Copy link

mcknight89 commented Oct 19, 2023

I just ran into this myself and I think the solution is to use an array instead e.g.

    const latestPostsEntries = await client.getEntries<BlogPostEntry>({
        content_type: 'blogPost',
        'fields.slug[ne]': slug, 
        order: ['-fields.releasedOn'], // Order by release date in descending order
        limit: 5, 
    });

Seems to work perfectly for me

@samzmann
Copy link

If that is the way to do it, then the docs should be updated. They don't mention the need for array brackets: https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters/order/query-entries/console/js

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

No branches or pull requests

4 participants