Generating types from your schema
Learn how to generate TypeScript types from your Sanity Studio schema using the Sanity CLI. This process will create a 'sanity.types.ts' file, providing you with types for all your schema, including built-in and plugin-related types.
With the schema.json
file created; you can now generate TypeScript types for your Sanity Studio schema. These types can be helpful in the Studio project, like customizations and contexts where you pass data that corresponds directly to the Studio schema types.
You do this by running this Sanity CLI command:
npx sanity typegen generate
npx sanity typegen generate
in your terminalWhen successfully completed, this command creates a sanity.types.ts
file containing TypeScript types for all your Sanity Studio schema types. Open it to find types for “built-in” schema types. If you have plugins with schema types, those will also be generated.
While these types can come in handy in your Studio project,
using them in your front end application is extremely useful to improve your development experience.
In the next chapter, you'll learn how to generate types for your front end project.