hdoro / sanity-plugin-prefixed-slug

Editor friendly slug fields for your Sanity.io studio
Apache License 2.0
34 stars 6 forks source link

Does not work with new `defineField()` and `defineType()` helpers #14

Open EricWVGG opened 1 year ago

EricWVGG commented 1 year ago

Sanity 3 has new Typescript helpers — defineType() and defineField() for defining documents, objects, and fields.

These helpers convert schemas into rich Typescript objects, along with all the usual advantages that come with that. It makes writing custom validators and "hidden" rules much easier and more feature-rich. (I imagine it has enormous advantages for writing custom components, as well.)

Unfortunately, these types do not allow custom options in schema objects, so this fails the Typescript validator:

import {defineType, defineField} from 'sanity'
import {SlugInput} from 'sanity-plugin-prefixed-slug'

export default defineType({
  name: 'article',
  type: 'document',
  title: 'Article',
  fields: [
    defineField({
      name: 'title',
      type: 'string',
      validation: (rule) => rule.required(),
    }),
    defineField({
      name: 'slug',
      type: 'slug',
      validation: (rule) => rule.required(),
      components: {
        input: SlugInput,
      },
      options: {
        source: 'title',
        urlPrefix: 'https://site.url', // < validator does not recognize this property
      },
    }),
  ],
})

… IMO @sanity-io should be typing options as Record<string, any> to allow for custom properties, but for whatever reason that's strict right now.

svey-xyz commented 1 year ago

I love the plugin but am having the same issue with Typescript. Just using // @ts-ignore above the urlPrefix for now as the studio will still compile without issue, but I would like to not have to do this.

Slgoetz commented 1 year ago

Pretty easy fix. They just need to pass options into the component components: { input: (props) => CustomSlugInput(props, prefixer), },

vivindeena commented 1 year ago

can you explain what do you mean by the prefixer

Pretty easy fix. They just need to pass options into the component components: { input: (props) => CustomSlugInput(props, prefixer), },

jawngee commented 1 month ago

You just need to add to your index.d.ts:

declare module 'sanity' {
  interface SlugOptions {
    urlPrefix?: string
  }
}