App SDK

Migrating from experimental TypeGen in App SDK

Learn how to use Sanity TypeGen with the App SDK for type-safe document and query hooks.

Unlike the deprecated experimental package, TypeGen doesn't currently infer types automatically when you call a hook. You generate types from your schema, then pass them explicitly through the hooks' generics.

To use TypeGen, keep a sanity.config.ts plus schema in your app directory (as with the experimental version).

Single project and dataset

Uses TypeGen's defaults — no config needed.

sanity schema extract      # → schema.json
sanity typegen generate    # → sanity.types.ts

Multiple resources

Make a workspace and schema for each project/dataset pair (the sanity.cli.ts typegen block holds only one schema, so use a config file per resource; you may receive a deprecation warning).

sanity schema extract --workspace primary   --path schema.primary.json
sanity schema extract --workspace secondary --path schema.secondary.json

Add the typegen config files, like this:

sanity typegen generate --config-path typegen.primary.json
sanity typegen generate --config-path typegen.secondary.json

Use the generated types

Import and pass them via the hook generics:

import type {Movie} from './sanity.types.primary'

const {data} = useDocument<Movie>({...movieHandle})

Limitations

Standard TypeGen generates result types for defineQuery queries but has no access to defineProjection that the experimental version does. For typed projection data, either type it by hand or use a useQuery with defineQuery and pass its generated result type.

Was this page helpful?