How to declare custom types in Sanity schema?

11 replies
Last updated: Jul 13, 2023
Hey everybody! Sorry to bother you all but I have an issue I cant figure out, even tho I know the solution is probably simple, and I'd like some guidance if possible:

How do you declare a custom type? For example in the Personal-Website-NextJS template, they have a custom type of "timeline" and "milestone", I want to understand how to do this but I keep getting errors :(
AI Update

No worries, this is a common question when starting with Sanity! Creating custom types is actually straightforward once you understand the pattern.

In Sanity, you define custom types in your schema files using the defineType helper function. For the Personal Website template's "timeline" and "milestone" types, here's how it works:

Basic structure for a custom document type:

import { defineType, defineField } from 'sanity'

export const timeline = defineType({
  name: 'timeline',
  type: 'document',
  title: 'Timeline',
  fields: [
    defineField({
      name: 'title',
      type: 'string',
      title: 'Title',
    }),
    // more fields...
  ],
})

For a custom object type (like milestone):

export const milestone = defineType({
  name: 'milestone',
  type: 'object',  // Note: 'object' not 'document'
  title: 'Milestone',
  fields: [
    defineField({
      name: 'description',
      type: 'string',
      title: 'Description',
    }),
    // more fields...
  ],
})

Key differences:

  • Document types (type: 'document') are top-level content that appear in your Studio and get their own IDs - these are standalone pieces of content
  • Object types (type: 'object') are reusable structures that exist within documents - like a milestone inside a timeline

Then register them in your schema:

In your sanity.config.ts (or schema/index.ts):

import { timeline } from './schemas/timeline'
import { milestone } from './schemas/milestone'

export default defineConfig({
  // ... other config
  schema: {
    types: [timeline, milestone],
  },
})

Common errors to watch for:

  • Not importing defineType and defineField from 'sanity'
  • Forgetting to register your types in the schema configuration
  • Using type: 'document' when you need type: 'object' (or vice versa)
  • Name conflicts - make sure your name field is unique

The defineType function gives you TypeScript autocomplete and helps catch errors early, so always use it even though it's technically optional! If you're still getting errors, double-check that you've registered your types in the schema array and that all your imports are correct.

Show original thread
11 replies
https://www.sanity.io/docs/schemas-and-forms
through the schema you can add custom types if I understand you correctly.
in the first example schema on this page it shows creating a new type called "person"
Just checked your link out. No, that's not what I was looking for.
Let me explain with this:
https://www.sanity.io/docs/schema-field-types (scroll down to the 4th code block)They didn't explain much on how they made the "accessibleImage" type or how they got it to work.
That's where I'm stuck .
they define that in the code block just before it
yes but when I reference it, I get an error that its not an appropriate type
Unknown type: timeline.
Valid types are: settings, array, block, boolean, datetime, date, document, email, file, geopoint, image, number, object, reference, crossDatasetReference, slug, string, telephone, text and url
have you included that type in your schema?
you need to import that type into your schema index file and add it to the array for it to be available
let me try again
ooof, it works now. thank you so much!I guess I was overthinking it. I assumed that 'objects' cant be referenced in the schema index, only 'documents' can. It's appearing and working fine now
πŸ™‚
πŸ™‚ yeah everything you define in the schema needs to be in that schema array, objects and all. glad you got it working πŸ‘

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?