Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript: next-sanity incompatible with @sanity-typed/types #1619

Open
elanderos318 opened this issue Jul 11, 2024 · 1 comment
Open

Typescript: next-sanity incompatible with @sanity-typed/types #1619

elanderos318 opened this issue Jul 11, 2024 · 1 comment

Comments

@elanderos318
Copy link

elanderos318 commented Jul 11, 2024

When using defineConfig export from @sanity-typed/types, typescript raises an error when passing the config to the config property to the NextStudio component. The types are incompatible, specifically the validator property in defineField:

Types of parameters 'rule' and 'rule' are incompatible. Type 'RuleDef<DocumentRule, SanityDocument>' is missing the following properties from type '{ custom: (fn: CustomValidator ...

Which versions of Sanity are you using?

@sanity/cli (global) 3.49.0 (latest: 3.50.0)
@sanity/demo 1.0.2 (latest: 2.0.0)
@sanity/vision 3.28.0 (latest: 3.50.0)
sanity 3.49.0 (latest: 3.50.0)

What operating system are you using?

macOS Sonoma 14.5

Which versions of Node.js / npm are you running?

10.5.0
v21.7.1

@josefdlange
Copy link

Here's how I worked around it in my case:

'use client'

/**
 * This configuration is used to for the Sanity Studio that’s mounted on the `/app/studio/[[...tool]]/page.tsx` route
 */

import {visionTool} from '@sanity/vision'
import {structureTool} from 'sanity/structure'
import {defineConfig as untypedDefineConfig} from 'sanity'

// Go to https://www.sanity.io/docs/api-versioning to learn how API versioning works
import {apiVersion, dataset, projectId} from './src/sanity/env'
import schemaTypes from '@/sanity/schemaTypes'
import {structure} from '@/sanity/structure'

import {defineConfig} from '@sanity-typed/types'
import {InferSchemaValues} from '@sanity-typed/types'

const CONFIG_PARAMS = {
    basePath: '/studio',
    projectId,
    dataset,
    // Add and edit the content schema in the './sanity/schemaTypes' folder
    schema: {types: schemaTypes},
    plugins: [
        structureTool({structure}),
        // Vision is for querying with GROQ from inside the Studio
        // https://www.sanity.io/docs/the-vision-plugin
        visionTool({defaultApiVersion: apiVersion}),
    ],
}

const UNTYPED_CONFIG_PARAMS = {...CONFIG_PARAMS, schema: {types: schemaTypes} as { types: any }}

export const untypedSanityConfig = untypedDefineConfig(UNTYPED_CONFIG_PARAMS);

const sanityConfig = defineConfig(CONFIG_PARAMS);

export default sanityConfig;


export type SanityValues = InferSchemaValues<typeof sanityConfig>

Basically in my sanity.config.ts I keep two versions of the config; one generated with this tool and one vanilla (untyped). I pass the untyped to the NextStudio instance and keep my typed versions around elsewhere. The critical line is where I cast {types: SchemaTypes} as { types: any }.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants