Extracting your schema
Learn how to extract your Sanity Studio schema into a JSON file. This is a crucial step before generating TypeScript types. This file, a static representation of your data's shape, will be used for type generation.
Before generating TypeScript types, you need to extract the Sanity Studio schema written in JavaScript into a static representation, a JSON file describing the shape of the data your Sanity Studio produces.
Run the following Sanity CLI command in your studio project folder:
npx sanity schema extract
sanity schema extract
in the Studio project from your terminalIf your command ran successfully, you should see a new file called schema.json
in your Studio project folder. In this course, you will only use this file for the type generation; you don’t need to do anything else with it.
If you explore its content, you will find your Studio schema described in a JSON format. For example, the document type for venue
will look something like this:
{ "name": "venue", "type": "document", "attributes": { "_id": { "type": "objectAttribute", "value": { "type": "string" } }, "_type": { "type": "objectAttribute", "value": { "type": "string", "value": "venue" } }, "_createdAt": { "type": "objectAttribute", "value": { "type": "string" } }, "_updatedAt": { "type": "objectAttribute", "value": { "type": "string" } }, "_rev": { "type": "objectAttribute", "value": { "type": "string" } }, "name": { "type": "objectAttribute", "value": { "type": "string" }, "optional": true }, "city": { "type": "objectAttribute", "value": { "type": "string" }, "optional": true }, "country": { "type": "objectAttribute", "value": { "type": "string" }, "optional": true } }}
This file can be used to develop tooling that needs to know about a Sanity Studio schema. But in this course, you will use it to generate TypeScript types.