Manually adding types for Contentful is a cumbersome experience especially when the content models within Contentful itself can be updated. To ensure you have the latest types from Contentful in your application there are a few scripts we can run.
Install the Contentful CLI globally
yarn global add contentful-cli
Login to Contentful
contentful login
Install cf-content-types-generator
yarn add cf-content-types-generator --dev
Update package.json
{
"scripts": {
"contentful:export": "contentful space export --export-dir contentful --content-file export.json --space-id XXX",
"contentful:typegen": "cf-content-types-generator contentful/export.json --v10 --out types/contentful"
}
}
Export Contentful Space
yarn contentful:export
Generate the Contentful types
yarn contentful:typegen
Summary
Within the directory types/contentful
you'll see files of generated types from Contentful that you can now use in your Typescript project.
Other Projects
contentful-typescript-codegen
contentful-ts-type-generator
contentful-ts-generator
TS Content Types Generator App
Top comments (0)