defineField type error with image field in Sanity schema
I can see you're hitting a TypeScript error saying that fields doesn't exist on the image type, even though the image type documentation clearly shows that fields is a supported property. This is frustrating because the functionality is definitely there!
Based on the documentation, the fields property absolutely works on image types. Here's the correct syntax from the official docs:
{
name: 'image',
title: 'Image',
type: 'image',
options: {
hotspot: true
},
fields: [
{name: 'title', title: 'Title', type: 'string'},
{name: 'alt', title: 'Alt Description', type: 'string'},
{name: 'attribution', title: 'Attribution', type: 'string'}
]
}If you're getting a TypeScript error despite this being valid, here are the most likely causes:
1. Missing or outdated Sanity type definitions
Make sure you have the latest @sanity/types package installed and that your Sanity packages are up to date:
npm install sanity@latest @sanity/types@latest2. Not using defineField consistently
Wrap both your outer image field AND the inner fields with defineField:
defineField({
name: 'mainImage',
type: 'image',
options: {
hotspot: true
},
fields: [
defineField({
name: 'alt',
type: 'string',
title: 'Alternative text',
}),
defineField({
name: 'caption',
type: 'string',
title: 'Caption',
})
]
})3. Type import issues
Ensure you're importing defineField from the correct package:
import {defineField, defineType} from 'sanity'4. IDE TypeScript server needs a restart
Sometimes your editor's TypeScript server gets out of sync. Try restarting it (in VS Code: Cmd/Ctrl + Shift + P β "TypeScript: Restart TS Server").
The fields property has been a standard feature of image types for a long time and follows the same pattern as fields on object types. Your code should work at runtime even if TypeScript is complaining, but getting the types right will give you proper autocomplete and type safety.
If none of these solve it, check if you have any custom type declarations that might be overriding the built-in Sanity types, or share your exact schema code and package versions for more specific help!
Show original thread4 replies
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.