I just encountered this error when trying to create an array with two types. Is this not possible, or am I doing something wrong? 😬

1 replies
Last updated: Jan 27, 2021
I just encountered this error when trying to create an array with two types. Is this not possible, or am I doing something wrong? 😬
AI Update

Arrays with multiple types are absolutely possible in Sanity! This is actually one of the powerful features of array fields. Without seeing your specific error, here are the most common issues and how to fix them:

The Basic Pattern

Arrays with multiple types should look like this:

{
  name: 'myArray',
  type: 'array',
  of: [
    {type: 'block'},
    {type: 'image'},
    {type: 'reference', to: [{type: 'author'}]}
  ]
}

Common Mistakes That Cause Errors

1. Invalid "of" property

The array type requires a valid "of" property that defines what types can be included. Make sure you have:

  • An of array (not type directly)
  • At least one valid type defined inside of

2. Type name conflicts

If you're defining inline object types within your array, their name properties shouldn't conflict with:

  • Global type names you've defined elsewhere in your schema
  • Built-in Sanity types like 'block', 'image', 'reference', etc.

For example, this would cause an error:

// āŒ Bad - 'callout' conflicts with a global type
of: [
  {
    name: 'callout',  // If you have a global 'callout' type
    type: 'object',
    fields: [...]
  }
]

3. Missing required properties

Each item in the of array needs at minimum a type property. If you're using references, they also need a valid to property configured correctly.

Working Example with Multiple Types

Here's a complete example that mixes different types:

{
  name: 'resourceItems',
  type: 'array',
  of: [
    {
      type: 'reference',
      to: [{type: 'blogPost'}]
    },
    {
      type: 'reference',
      to: [{type: 'video'}]
    },
    {
      type: 'object',
      name: 'customNote',
      fields: [
        {name: 'text', type: 'string'}
      ]
    }
  ]
}

If you share the specific error message you're seeing, I can help pinpoint the exact issue! The error message usually gives good hints about what's wrong with the schema configuration.

It's a bit subtle, but
image
Ā is an actually an object type, and you can't mix primitive types (like numbers and strings) with object types. That means that you have to put your
string
Ā inside of an object. So:
of: [ { type: 'object', fields: [{type: 'string', name: 'text'}]}, { type: 'image' }]

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?