Extending Sanity defineType with custom properties in TypeScript
I understand you want to add custom properties like codegen: { required: true } to your schema definitions, but I need to be upfront: there isn't an officially documented way to extend type definitions with custom properties in Sanity's defineType and defineField functions.
The defineType and defineField functions are TypeScript helper functions designed to provide autocomplete and type checking for Sanity's standard schema properties. They don't officially support extending the type definitions with arbitrary custom properties.
What you can do instead
1. Use Sanity TypeGen (the official approach):
Instead of trying to control code generation through custom schema properties, use Sanity's built-in TypeGen which generates TypeScript types directly from your schema:
npx sanity schema extract
npx sanity typegen generateThis generates proper TypeScript types from your schema. Fields with .required() validation will automatically be reflected as required (non-optional) in the generated types—no custom properties needed:
import { defineField, defineType } from 'sanity'
export const myDocument = defineType({
name: 'myDocument',
type: 'document',
title: 'My Document',
fields: [
defineField({
name: 'title',
type: 'string',
title: 'Title',
validation: (rule) => rule.required(), // This makes it required in generated types
})
]
})2. Consider third-party tools like sanity-codegen:
If you need different code generation behavior, this community plugin offers alternative configuration options, though it also works from the standard schema definition rather than custom properties.
3. Use TypeScript's type assertions (workaround, not recommended):
If you absolutely must add custom properties for your own tooling, you can suppress TypeScript errors:
defineField({
name: 'title',
type: 'string',
codegen: { required: true }, // Your custom property
validation: (rule) => rule.required(),
} as any) // Suppresses TypeScript errorsHowever, this is not an officially supported pattern and could break in future versions.
Why custom properties aren't supported
Sanity's schema definitions are runtime configuration objects that tell Studio how to behave. The TypeScript types for these definitions are intentionally strict to ensure you're only using supported properties. Adding arbitrary properties would either be ignored by Sanity or cause type errors (which you're experiencing).
The official TypeGen tool works by extracting and analyzing your actual schema structure—including validation rules—not by reading custom metadata properties. This keeps the schema focused on what Sanity needs to function while still generating accurate TypeScript types.
If you have a specific use case for why you need codegen: { required: true } separate from validation rules, there might be an alternative approach that fits better with Sanity's architecture!
Show original thread2 replies
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.