Migrating from TypeGen Beta to GA

Migrating from TypeGen Beta to GA

Learn how to migrate your TypeGen beta setup to the GA release. Understand breaking changes, update your configuration, and leverage new features.
Log in to mark your progress for each Lesson and Task

If you used TypeGen during the beta period, the GA release includes some breaking changes that improve type accuracy and developer experience. This lesson guides you through the migration.

Ensure you're running the latest version:

npm install sanity@latest

The old sanity-typegen.json file is deprecated. Move all configuration to sanity.cli.ts:

Old (sanity-typegen.json):

{
"path": "./src/**/*.{ts,tsx}",
"schema": "./schema.json",
"generates": "./src/types.ts"
}

New (sanity.cli.ts):

import {defineCliConfig} from 'sanity/cli'
export default defineCliConfig({
schemaExtraction: {
enabled: true,
},
typegen: {
enabled: true,
path: './src/**/*.{ts,tsx}',
schema: './schema.json',
generates: './src/types.ts'
}
})

Run the generation command:

npx sanity typegen generate

Expect to see diffs in your generated types file due to improvements:

Enable schemaExtraction.enabled: true to generate schema.json automatically during sanity dev and sanity build. If you also want automatic type generation in the studio, add typegen.enabled: true as well. For frontend packages that don't use sanity dev/build, run sanity typegen generate --watch directly.

  1. Preserved Casing: Type names now match your query variable names exactly
  2. Quoted Keys: Fields with hyphens or special characters are properly quoted
  3. Configuration Location: All config must be in sanity.cli.ts

These changes make generated types more accurate and TypeScript-compliant.

After migration:

  1. Verify types generate without errors
  2. Check that your queries still type correctly
  3. Update any code that relied on old type names
  4. Test automatic generation with enabled: true

If you encounter issues during migration, visit the #typescript channel in Sanity Community Discord.

The breaking changes in GA are intentional improvements that make TypeGen more reliable and accurate. The migration is straightforward and unlocks significant workflow improvements.