sanity icon indicating copy to clipboard operation
sanity copied to clipboard

TypeGen does not generate correct type when querying an array containing multiple references types

Open obrassard opened this issue 1 month ago • 1 comments

I have a document containing an array field, mixing different types of reference and objects :

defineField({
      name: 'modules',
      title: 'Modules',
      type: 'array',
      group: 'editorial',
      of: [
        { type: 'reference', name: 'ref.module.fullWidthTextBlock', to: [{ type: 'module.fullWidthTextBlock' }] },
        { type: 'reference', name: 'ref.module.textWithImageBlock', to: [{ type: 'module.textWithImageBlock' }] },
        { type: 'reference', name: 'ref.module.fullWidthImageBlock', to: [{ type: 'module.fullWidthImageBlock' }] },
        // ... Other reference types here
        { type: 'module.instagramBlock' }, // Object
      ],
    }),

We fetch this data using a query like this :

export const SanityProductModulesQuery = groq`
*[_type == 'product' && store.slug.current == $handle][0].modules[] {
    _type == 'module.instagramBlock' => {
        _key,
        _type,
        "title": title[$lang],
        category,
    },
    _type == 'ref.module.textWithImageBlock' => @ -> {
        _type,
        "_key": ^._key,
        "subTitle": subTitle[$lang],
        "title": title[$lang],
        "body": body[$lang],
        color,
        orientation
    },
    _type == 'ref.module.fullWidthTextBlock' => @ -> {
        _type,
        "_key": ^._key,
        "title": title[$lang],
        "body": body[$lang],
    }
}
`;

The Generated Types for this query is incorrect :

export type SanityProductModulesQueryResult = Array<
  | {
      _key: string;
      _type: 'module.instagramBlock';
      title: Array<{
        _type: 'localizedString';
        fr: string;
        en: string;
      }>;
      category: string;
    }
  | {} //We should have other types here, for referenced items
> | null;

The query itself work properly when tested in the vision plugin.


Which versions of Sanity are you using?

@sanity/cli (global)          3.45.0 (up to date)
@sanity/asset-utils            1.3.0 (up to date)
@sanity/color-input            3.1.1 (up to date)
@sanity/eslint-config-studio   3.0.1 (latest: 4.0.0)
@sanity/icons                  3.2.0 (up to date)
@sanity/language-filter        4.0.2 (up to date)
@sanity/ui                     2.3.1 (latest: 2.3.3)
@sanity/vision                3.45.0 (up to date)
sanity                        3.45.0 (up to date)

What operating system are you using? Mac OS 14.5 / Chrome

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

NPM : 9.8.1 NODE : v18.18.2

obrassard avatar Jun 12 '24 16:06 obrassard