Content types
Generate TypeScript types from your collection schemas, and keep them up to date in CI.
The CLI can generate TypeScript types from the schemas of your collections. With them, the SDK knows the properties and fragments of each collection, so your editor completes field names and finds mistakes.
Generate types
Run
npx andesine initand choose the content source, the collections, and the output file. See CLI.Run:
The default output file is src/andesine.generated.ts. Commit it with your code.
Use the types
The generated file exports a Workspace type for the client, and types for each collection, such as TutorialsCollection, TutorialsCollectionContent, and TutorialsCollectionStructuredContent:
How fields become types:
Required fields are required keys. Optional fields are optional keys.
Numbers are
number | null. A select is one of its options or"". A multi-select is an array of its options.Fragments allow only the block types of the fragment.
Elements have general types, because schemas do not define them.
Options
Option | Does |
|---|---|
| Uses the current schemas instead of the published ones |
| Uses the schemas of one fixed publish |
| Limits the types to collections, by path or ID |
| Adds the paths of the entries as types |
| Adds the IDs of the entries as types |
| Adds the order of the collection tree as types |
Flags apply only to that run. Use andesine init to save them.
Keep types up to date
Check in CI that the committed types match the schemas. Set ANDESINE_API_KEY as a secret, and run:
The command exits with code 3 when the types are missing or out of date, and writes no files.
While you work, regenerate the types when a schema changes:
Watch mode checks every 10 seconds. Use --interval to change it.