Clarification on the correct usage of `defineField` and `defineArrayMember` in a Slack thread.

7 replies
Last updated: Aug 29, 2023
defineField({
      title: "Navigation",
      description: "Select pages or link for main navigation",
      name: "navigation",
      type: "array",
      group: "navigation",
      of: [
        defineField({
          title: "Internal Link",
          name: "internalLink",
          type: "internalLink"
        }),
        defineField({
          title: "External Link",
          name: "externalLink",
          type: "externalLink"
        })
      ]
    }),
Quick question, isn't this supposed to be defineArrayMember instead of defineField on the type errors?
AI Update

Yes, you're absolutely right! In the of array of an array field, you should use defineArrayMember instead of defineField.

The corrected version should be:

defineField({
  title: "Navigation",
  description: "Select pages or link for main navigation",
  name: "navigation",
  type: "array",
  group: "navigation",
  of: [
    defineArrayMember({
      title: "Internal Link",
      name: "internalLink",
      type: "internalLink"
    }),
    defineArrayMember({
      title: "External Link",
      name: "externalLink",
      type: "externalLink"
    })
  ]
})

The key distinction is:

  • defineField is for defining regular schema fields at the top level or within object types
  • defineArrayMember is specifically for defining the types of items that can be contained within an array field's of property

Using defineArrayMember provides proper TypeScript typing support for array members. While your schema might work with defineField in some cases, defineArrayMember is the correct and recommended approach for array members and will give you better type safety and IDE support.

I believe
defineArrayMember
is the right one 👍
Thank you
So to confirm anything inside OF or TO is an arrayMembrer?
if the parent type is array the of or to is an arrayMember correct?
you know what, I don’t think the
to
array is typed at all . It only supports simple
{type: 'documentTypeName'}
objects, so my guess is that we didn’t see it as super useful
so
defineArrayMember
is only for objects in the
of
array
Great thank you very much!

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?