How-to guide

Flexible schemas
Type-safe queries

Sanity features in this guide

TypeGen

TypeGen Docs

Sanity Studio

Learn more

GROQ

GROQ Docs

The best of both worlds

If you have an existing Sanity project, you may prefer to read the TypeGen Docs for details on how to incorporate type-safety.

What's covered in this guide

You'll create a new free Sanity project, and generate TypeScript types for the Sanity Studio schema types and your front-end's GROQ queries.

Create a new project

From the command line, create a new free Sanity project using the Next.js template.

If you don't already have an account with Sanity, you'll be prompted to create one.

Now inside this directory, start the development server.

You should now have two apps running:

These apps are separately configured in the following directories:

├─ /nextjs-app
└─ /studio

The Sanity Studio is configured with code, edits you make to the configuration will render immediately in the development environment.

Content in Sanity Studio is written to a schemaless, cloud-hosted Dataset in what we call the Content Lake.

The Next.js app fetches content using Sanity Client with the GROQ query language.

Update Studio schema types

In your browser, open Sanity Studio. Click to the Structure tool in the header and create a new post type document.

In your code editor, open the schema type file for post type documents – post.ts – and add a new field called readingTime.

You should now see this field when editing a post type document.

Input field showing the word "reading time"

Update your types

TypeGen requires two steps to complete:

  1. A static "extraction" of your Sanity Studio schema configuration into JSON.
  2. Creating Types based on that schema and any GROQ queries the process finds in your code into TypeScript.

Extract your schema types

To update your Types, you'll first need to update the static extraction file from your Sanity Studio

Inside the Studio directory, run the following command:

Inside your Next.js directory, run the following command:

You should see a confirmation message like.

Now in the Next.js app, open app/components/Posts.tsx and see how the PostType has been extended to include your new readingTime field.

Code showing accessing a value from an object in TypeScript

However, PostType is a representation of the Sanity Studio schema, and may not match what your GROQ query requested. It is better to rely on the generated Types for query responses.

Update your GROQ query types

Sanity supports GraphQL, but GROQ is more widely used to query for Sanity content.

Open queries.ts and see how the Next.js app has multiple GROQ queries for Sanity content.

Inside the postFields variable, add the following line to the projection. This will use the value of your new readingTime field, to a return a boolean if it is not greater than 10.

Code highlighting a new line in a GROQ query

Inside your Next.js directory, update the Types for query responses by re-running TypeGen:

Now in Posts.tsx, you can update:

  • PostProps to use the AllPostsQueryResult type
  • Post to extract isLongRead from the props

Now the attributes available on the post prop match the GROQ query you wrote to fetch them.

Code demonstrating accessing an attribute from an object with TypeScript

Summary

You've now successfully created a new Sanity project, with a locally configured Sanity Studio.

You're able to rapidly update the schema types for content creators as well as query for that content in creative ways using GROQ.

All maintaining type-safety up and down your stack.

Go deeper

More resources

Course • Sanity Learn

Day one content operations

Get a top-level understanding of the entire Sanity Content Operating System while building out a multi-application monorepo. With callouts to take other course material to go deeper.

View course

What's next?

TypeGen, Sanity Studio and GROQ and are just a few parts of the Sanity Content Operating System. Get a fresh start on a new project or take a guided course for more.

Explore more guides

Sanity TypeGen

Flexible schemas, Type-safe queries

Visual Editing

Fix a typo without a PR

Live Content API

Really easy real-time