Debugging Groq query type issue in Sanity typegen

7 replies
Last updated: Jun 7, 2024
I have a Groq query that works perfectly fine, but the generated type from sanity typegen gives me
Array<never>
.
Is this a good place to debug this? I’ll put more details in the thread.
AI Update

Yes, this is a good place to debug this! The Array<never> type issue with Sanity TypeGen is a known problem that usually has a straightforward fix.

Based on the community thread with this exact issue, the most common cause is using an outdated version of the Sanity CLI. The person who originally posted that exact problem solved it by updating to the latest CLI version and re-running type generation.

Here's what you should try:

1. Update your Sanity CLI

npm install -g @sanity/cli@latest
# or if you have it as a dev dependency
npm install -D @sanity/cli@latest

2. Re-run type generation

npx sanity typegen generate

Common causes of Array<never>:

  • Outdated CLI version - TypeGen has had several improvements for handling complex queries, especially those with references and dereferencing (the -> operator)
  • Complex reference chains - Queries with -> dereferencing like mainPostType->slug.current have historically caused issues in older versions
  • Schema not in sync - Make sure your schema changes are saved

Additional troubleshooting steps if updating doesn't fix it:

  1. Verify your query uses defineQuery - TypeGen requires queries to be wrapped with defineQuery to be discoverable:

    import { defineQuery } from 'next-sanity' // or 'groq'
    
    const MY_QUERY = defineQuery(`*[_type == "post" && mainPostType->slug.current == "guides"]`)
  2. Check your CLI version - Run npx sanity --version to confirm you're on the latest

  3. Ensure the query variable name is unique - TypeGen uses the variable name to generate the type (e.g., MY_QUERYResult), so it needs to be globally unique in your project

  4. Verify the query is syntactically valid - Test it in Vision first to ensure it returns results

In the original thread, the issue was specifically with queries containing mainPostType->slug.current, and updating the CLI immediately resolved it. The TypeGen system has gotten much better at handling reference dereferencing in recent versions, so this should fix your issue too!

Feel free to share more details (CLI version, actual query code, any error messages) if you're still having trouble after updating.

The Groq query. Screenshot of Vision attached so we can see it does work.

*[_type == "post" && mainPostType->slug.current == "guides"]|order(publishedAt desc){
  title
}
If I remove
&& mainPostType->slug.current == "guides"
the generated type is correct.
👋 What version of the CLI are you using?
🤦 I updated to the latest, re-ran generate, and it’s good to go now.
Thanks for the fast reply, y’all are great.
Glad it was an easy fix!
I am having the same issue with all the latest versions. Any help please?

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?