Sanity
Learn
CoursesTyped content with Sanity TypeGenGenerating types from your schema
Track
Sanity Developer Certification

Typed content with Sanity TypeGen

Lesson
3

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.

Log in to mark your progress for each Lesson and Task

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:

Terminal
npx sanity typegen generate
Run npx sanity typegen generate in your terminal

When 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.