contentful-userland / cf-content-types-generator

Generate TS declarations for content types
MIT License
115 stars 25 forks source link

"Type does not satisfy the constraint 'EntrySkeletonType'." #262

Closed dtrenz closed 1 year ago

dtrenz commented 1 year ago

When using generated types w/ contentful: ^10.2.3 (using cf-content-types-generator: ^2.12.2):

Type '<Generated Type>' does not satisfy the constraint 'EntrySkeletonType'. 
Type '<Generated Type>' is missing the following properties from type 'EntrySkeletonType': fields, contentTypeId
veu commented 1 year ago

Hi, we do need a bit more info to help you out.

BernardHymmen commented 1 year ago

I just ran into the exact same issue. This is my first time trying to do type generation for Contentful objects so I was just gettting started and following the steps laid out in this very nice blog post

https://www.seancdavis.com/posts/generating-workable-typescript-types-from-contentful-content/

There's no date on that post (boo!), but it looks like the embedded video was uploaded in early May, FWIW. Anyway, don't recall making any changes to what the post describes, other than consolidating it all into a single npm script like so:

    "contentful-export": "contentful space export --config contentful/export-config.json --management-token %CONTENTFUL_MANAGEMENT_PAT% --space-id %CONTENTFUL_SPACE_ID%",
    "contentful-typegen": "cf-content-types-generator contentful/export.json --out types/contentful",
    "contentful-update": "npm run contentful-export && npm run contentful-typegen"

Other than that, the post describes my setup and process accurately. I've got a pre-build.cmd script that I run as part of my build to automatically update my Contentful models for both my C# and (hopefully soon) TypeScript types. Here's the command for the TypeScript part of it:

call npm run contentful-update

This process generates a JSON schema file that looks just fine to my casual eye, and the TypeScript type definitions all look fine too...except for the red squiggles in VSCode on the type parameter in every instance of Entry<T>. Here's an example of one of my objects...

import type { Asset, Entry, EntryFields } from "contentful";
import type { TypePersonFields } from "./TypePerson";

export interface TypeBlogPostFields {
    title: EntryFields.Symbol;
    slug: EntryFields.Symbol;
    heroImage?: Asset;
    description: EntryFields.Text;
    body?: EntryFields.Text;
    author?: Entry<TypePersonFields>;  // <== squiggles on TypePersonFields
    publishDate: EntryFields.Date;
    project?: Entry<Record<string, any>>[];  // <== squiggles on Record<string, any>
}

export type TypeBlogPost = Entry<TypeBlogPostFields>; // <== squiggles on TypeBlogPostFields

All the error messages are versions of

Type 'TypePersonFields' does not satisfy the constraint 'EntrySkeletonType'. Type 'TypePersonFields' is missing the following properties from type 'EntrySkeletonType': fields, contentTypeId ts(2344)

Here are the various package versions I'm using:

dependencies
    "contentful": "^10.2.3",
    "contentful-cli": "^2.6.22",

devDependencies
    "cf-content-types-generator": "^2.12.2",
    "typescript": "^4.7.4"
veu commented 1 year ago

@BernardHymmen To generate types for v10 you need to add the --v10 or -X option. That will create the new EntrySkeleton format and you’re good to go.

BernardHymmen commented 1 year ago

@veu - Thanks for your help! Yes, adding the --v10 option did the trick for me. Yay!

I thought you might want to know that, curiously, the -x option threw an error for me. Here's what I got:

> cf-content-types-generator contentful/export.json -x --out types/contentful

 »   Error: Unexpected argument: -x
 »   See more help with --help
SimonyanGrno commented 1 year ago

@BernardHymmen I've used uppercase X and everything works fine

BernardHymmen commented 1 year ago

@SimonyanGrno - D'OH! All the other arguments are lowercase and I failed to notice that -X is, in fact, uppercase. <FacePalm/> Yes, it worked just fine when I switched it to uppercase. Thanks for pointing this out!

marcolink commented 1 year ago

This issue seems to be solved - will close for now