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.
Run the following Sanity CLI command in your studio project folder:
Terminal
npx sanity@latest typegen generate
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.
Schema Types are especially useful for type safety when creating or patching documents in custom scripts or functions.
import type { Event } from './sanity.types.ts'
await client.create<Event>({ _type: "event", _id: uuid(), // ... and other fields})
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.
You have 1 uncompleted task in this lesson
0 of 1