Error when hoisting annotation to its own type in Slack thread.

8 replies
Last updated: Jun 1, 2021
Adding an object to annotations in a block is throwing an error when I hoist the annotation to it’s own type.

Uncaught errorCould not resolve jsonType of undefined. No parent type found


annotations: [

{

name: "href",

title: "URL",

type: "externalLink",

icon: MdLink

}

],

It’s referencing this simple object as a test.


export default {

name: 'externalLink',

title: 'External Link',

type: 'object',

fields: [

{

name:'foo',

title: 'foo',

type: 'string'

}

]

}


If I simply add the annotation in-line everything works fine.


annotations: [

{

name: "color",

title: "Text Color",

type: "color",

icon: MdFormatColorText,

},

{

name: 'externalLink',

title: 'External Link',

type: 'object',

fields: [

{

name:'foo',

title: 'foo',

type: 'string'

}

]

}

]


Any suggestions?
AI Update

The error you're encountering is a common schema registration issue. When you reference a custom type like externalLink in your annotations, Sanity needs to know about that type definition beforehand. The type must be registered in your schema before it can be referenced.

The key issue is that your externalLink type definition needs to be included in your schema's types array. When you define it inline, Sanity automatically registers it, but when you extract it to its own file, you need to explicitly add it to your schema configuration.

Here's how to fix it:

1. Make sure your externalLink type is exported from its file:

// schemas/externalLink.js (or .ts)
export default {
  name: 'externalLink',
  title: 'External Link',
  type: 'object',
  fields: [
    {
      name: 'foo',
      title: 'Foo',
      type: 'string'
    }
  ]
}

2. Import and register it in your main schema configuration:

// sanity.config.js (or schema/index.js depending on your setup)
import externalLink from './schemas/externalLink'
import yourBlockContentType from './schemas/yourBlockContentType'

export default defineConfig({
  // ... other config
  schema: {
    types: [
      externalLink,  // Register the type here BEFORE using it
      yourBlockContentType,
      // ... other types
    ],
  },
})

3. Now reference it in your annotations:

annotations: [
  {
    name: "href",
    title: "URL",
    type: "externalLink",  // This will now resolve correctly
    icon: MdLink
  }
]

The error "Could not resolve jsonType of undefined. No parent type found" occurs because Sanity is trying to find a type called externalLink but can't find it in the registered schema types. By adding it to the types array in your schema configuration, you're making it available for reference throughout your schema.

This is the standard pattern for all custom types in Sanity - whether for annotations, regular fields, or references. All custom types must be registered in the schema before they can be referenced elsewhere. The inline version works because Sanity automatically registers inline type definitions within the context where they're used.

Hey User! Just to double check, did you add
externalLink
to your schema.js? If that’s not the issue, I believe that adding a new parameter (
icon
) may actually create a new type.
I did, however I noticed the following….
This is how I’m importing objects into my schema
// Object Types

import * as objects from './objects'

const allObjects = Object.values(objects).map((myObject) => {

return { ...myObject }

})
_export_ default createSchema({

// We name our schema

name: 'default',

// Then proceed to concatenate our document type

// to the ones provided by any plugins that are installed

types: schemaTypes.concat([

/* Your types here! */

page,

section,

sectionBlock,

config,

theme,

nav,

sponsor,

])

.concat(allObjects)

.concat(allSections)

.concat(allConfigs)

.concat(allPlugs)

})
I just tried to import it manually into the file without using the allObjects constant and it worked. So that looks like the following, but it feels like a workaround.
// Manual object imports

import link from './objects/link'




// Then we give our schema to the builder and provide the result to Sanity

_export_ default createSchema({

// We name our schema

name: 'default',

// Then proceed to concatenate our document type

// to the ones provided by any plugins that are installed

types: schemaTypes.concat([

/* Your types here! */

page,

section,

sectionBlock,

config,

theme,

nav,

sponsor,

link,

// backgroundImage,

// simpleBlockContent,

// palette,

// typography,

])

.concat(allObjects)

.concat(allSections)

.concat(allConfigs)

.concat(allPlugs)

})
I’m really confused why manually importing works while using the allObject constant doesn’t.

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?