Clarification on the correct usage of `defineField` and `defineArrayMember` in a Slack thread.
7 replies
Last updated: Aug 29, 2023
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:
defineFieldis for defining regular schema fields at the top level or within object typesdefineArrayMemberis specifically for defining the types of items that can be contained within an array field'sofproperty
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.
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.