contentful.js icon indicating copy to clipboard operation
contentful.js copied to clipboard

Types issue

Open damian-turek opened this issue 1 year ago • 5 comments

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

damian-turek avatar Sep 19 '23 08:09 damian-turek

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

marcolink avatar Oct 11 '23 11:10 marcolink

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 [email protected] (latest as of today): https://github.com/contentful-labs/cf-content-types-generator/

samzmann avatar Oct 16 '23 15:10 samzmann

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

mcknight89 avatar Oct 19 '23 09:10 mcknight89

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

samzmann avatar Oct 19 '23 09:10 samzmann

Concur with the above - the only place where I've seen the need for an array mentioned is on the TypeScript-specific docs page:

https://github.com/contentful/contentful.js/blob/master/TYPESCRIPT.md#breaking-change

Query types that accept an array of values used to accept them as a comma-separated string which is no longer supported. The user must instead provide the original array.

The list of query filters that only accept arrays from now on:

select order [within] [near] [in] [nin]

mbifulco avatar Jul 29 '24 20:07 mbifulco