Extending Sanity defineType with custom properties in TypeScript

2 replies
Last updated: Sep 5, 2023
Hello friends!Is it possible to extend the type definitions when using the
defineType
function in Sanity? I would for example be interested in having the property
codegen: { required: _true_ }
on field types. I am of course getting typescript errors in vscode when I try to add that property.
AI Update

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 generate

This 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 errors

However, 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 thread
2 replies
For anyone reading this I found the solution. 🎉
Wrap the field definition in
defineField
(which I guess is best practice anyway). Set
strict: false
as such:

defineField({
  title: 'Title',
  name: 'title',
  type: 'string',
  codegen: { required: true },
  validation: (rule) => rule.required(),
}, {
  strict: false
}),
Thanks

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.

Was this answer helpful?