Migrating from TypeGen Beta to GA
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@latestThe 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 generateExpect to see diffs in your generated types file due to improvements:
- Query names now preserve casing (
MyPageQuery→MyPageQueryResult) - Non-identifier keys are properly quoted (
'my-field'instead ofmy-field) - Array types use
ArrayOfutility for smaller output
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.
- Preserved Casing: Type names now match your query variable names exactly
- Quoted Keys: Fields with hyphens or special characters are properly quoted
- Configuration Location: All config must be in
sanity.cli.ts
These changes make generated types more accurate and TypeScript-compliant.
- Automatic type generation during
sanity dev - Faster watch mode with memoization
- New
GetandFilterByTypeutilities - Better framework support (SvelteKit, Vue, Svelte)
- Stable API that won't break in future updates
After migration:
- Verify types generate without errors
- Check that your queries still type correctly
- Update any code that relied on old type names
- 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.