Closed dtrenz closed 1 year ago
Hi, we do need a bit more info to help you out.
<Generated Type>
look like?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"
@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.
@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
@BernardHymmen I've used uppercase X and everything works fine
@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!
This issue seems to be solved - will close for now
When using generated types w/
contentful: ^10.2.3
(usingcf-content-types-generator: ^2.12.2
):