Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Issue with typegen and boolean filtering in qroq queries

9 replies
Last updated: Apr 24, 2024
I’m encountering a strange behavior with the typegen. A qroq query like this (where root is a boolean):
*[_type == 'page' && root != true]
generates the following:

pages: Array<never>
To fix this, I have to explicitly filter by root == false, but this doesn’t account for page where the boolean is not set.
Apr 5, 2024, 11:29 AM
Hey! hmmm, I can't seem reproduce this behavior. Do you have a schema type called
page
?
Apr 5, 2024, 11:45 AM
Yes I do
import { defineField, defineType } from 'sanity'

export default defineType({
  title: 'Page',
  name: 'page',
  type: 'document',
  fields: [
    defineField({
      name: 'title',
      type: 'string'
    }),
    defineField({
      name: 'slug',
      type: 'slug',
      options: {
        source: 'title',
        maxLength: 96
      }
    }),
    defineField({
      name: 'root',
      type: 'boolean'
    }),
    defineField({
      name: 'modules',
      type: 'array',
      of: [{ type: 'moduleText' }]
    })
  ]
})
Apr 5, 2024, 11:48 AM
My rootQuery looks like this:
export const rootQuery = groq`{
  'languages': *[_type == 'language'],
  'tags': *[_type == 'tag'],
  'pages': *[_type == 'page' && root != true]
}`
Apr 5, 2024, 11:49 AM
Ah. I can reproduce it
Apr 5, 2024, 11:49 AM
The generated RootQueryResult
export type RootQueryResult = {
  languages: Array<{
    id: string | null;
    title: string | null;
  }>;
  scripts: Array<{
    _id: string;
    title: string | null;
    slug: string | null;
  }>;
  tags: Array<{
    _id: string;
    title: string | null;
    slug: string | null;
  }>;
  pages: Array<never>;
};
Apr 5, 2024, 11:51 AM
I've reproduced it, and got a fix lined up - not sure when we'll get it out, but hopefully we'll get it sorted next week
Apr 6, 2024, 12:53 PM
Great to hear and thank you for looking into it so quickly.
Apr 6, 2024, 2:23 PM
Hi! Sorry it took some time, but this should now be fixed in our type evaluator, it will be part of next weeks release of our sanity cli.To test it now you can delete your lock file, and it should pick up the latest version as it's a minor release
Apr 24, 2024, 12:25 PM
Thanks for the update Sindre 🙌
Apr 24, 2024, 12:45 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?