Skip to content

Spreading array into array creates wrong TS type #270

@heggemsnes

Description

@heggemsnes

If you find a security vulnerability, do NOT open an issue. Email [email protected] instead.

Describe the bug

Doing a projection like this:

"categories": array::compact([
        ...relatedProducts[]->{
            _id,
            "title": coalesce(title, name),
        },
        ...relatedIndustries[]->{
            _id,
            "title": coalesce(title, page.title),
        },
        ...relatedApplicationUseCases[]->{
            _id,
            "title": coalesce(title),
        }
    ]),

Causes the type generated type to be:

({
    _id: string;
    title: string;
}[] | {
    _id: string;
    title: string | null;
}[])[]

Whereas the data is correct with the objects in the top array:

categories: [
  { _id: '31827a4f-1b1a-4eed-90e6-a1475bfd9250', title: 'MRU Marine' },
  {
    _id: 'be1d8610-2df5-4867-8cfb-3651f8d21660',
    title: 'MRUs for Shipping'
  },
  { _id: 'fce3bc20-41ca-4c1e-8a83-0c48499f3515', title: 'Wind energy' }
]

To Reproduce

Steps to reproduce the behavior:

Query something with an array field and spread the array into a constructed array in groq.

Expected behavior

Type looks the same as data when spreading arrays.

Which versions of Sanity are you using?

@sanity/cli (global) 3.70.0 (latest: 3.71.2)
@sanity/icons 3.5.7 (up to date)
@sanity/image-url 1.1.0 (up to date)
@sanity/orderable-document-list 1.2.2 (up to date)
@sanity/preview-url-secret 2.1.0 (latest: 2.1.3)
@sanity/ui 2.11.1 (latest: 2.11.6)
@sanity/vision 3.69.0 (latest: 3.71.2)
@sanity/visual-editing 2.12.0 (latest: 2.12.9)
sanity 3.70.0 (latest: 3.71.2)

What operating system are you using?

MacOS 14.3.1

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

v20.11.0, pnpm 9.6.0

Run npm -v && node -v in the terminal and copy-paste the result here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions