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

TypeGen generates Array<never> for query using "in" #6628

Closed
oyvind-stenhaug opened this issue May 13, 2024 · 4 comments · Fixed by #7424
Closed

TypeGen generates Array<never> for query using "in" #6628

oyvind-stenhaug opened this issue May 13, 2024 · 4 comments · Fixed by #7424
Assignees
Labels
bug typegen Issues related to TypeScript types generation

Comments

@oyvind-stenhaug
Copy link

oyvind-stenhaug commented May 13, 2024

If you find a security vulnerability, do NOT open an issue. Email security@sanity.io instead.

Describe the bug

When a GROQ query uses the in operator in a filter component, the TypeGen-generated type of the query result ends up as Array.

To Reproduce

  1. Create a project (opting in to TypeScript) or edit an existing one, and make a sanity.config.ts like the following:
import {defineConfig, defineType, defineField} from 'sanity'

export default defineConfig({
  name: 'default',
  title: '<your_project_title>',
  projectId: '<your_project_id>',
  schema: {
    types: [
      defineType({
        type: 'document',
        title: 'Test',
        name: 'test',
        fields: [
          defineField({
            name: 'foo',
            type: 'string',
          }),
        ],
      }),
    ],
  },
})
  1. Create a src/queries.ts file with these contents:
const queryWithIn = groq`
  *[_type == "test" && foo in ["bar", "baz"]]{foo}
`
  1. Run typegen, e.g. npx sanity schema extract && npx sanity typegen generate && cat sanity.types.ts, and observe the QueryWithInResult type

Expected behavior

export type QueryWithInResult = Array<{
  foo: string | null
}>

(which is the result generated if you use a filter that involves == instead of in)

Actual behavior

export type QueryWithInResult = Array<never>

Which versions of Sanity are you using?

@sanity/cli (global)          3.41.1 (up to date)
@sanity/eslint-config-studio   4.0.0 (up to date)
@sanity/vision                3.41.1 (up to date)
sanity                        3.41.1 (up to date)

What operating system are you using?

GNU/Linux (Ubuntu 22.04.4 LTS)

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

npm -v && node -v:

10.2.4
v20.11.1

@oyvind-stenhaug
Copy link
Author

For what it's worth, this is a simplified case. My actual query is more like

*[_type == "mainDocumentType" && slug.current == $slug]{
  ...,
  portableTextField[]{
    ...,
    _type == "someCustomBlockType" => {
      _type,
      "nested": *[
        _type == "anotherDocumentType" && type in ^.types && (...more stuff)
      ]{
        ${NESTED_QUERY_PROJECTION}
      }
    },
    ${PORTABLE_TEXT_PROJECTION}
  }
}[0]

@rexxars rexxars added the typegen Issues related to TypeScript types generation label May 14, 2024
@bjoerge bjoerge removed the typegen Issues related to TypeScript types generation label May 22, 2024
@sgulseth sgulseth added bug typegen Issues related to TypeScript types generation labels May 22, 2024 — with Linear
@sgulseth sgulseth self-assigned this Jul 26, 2024
@sgulseth
Copy link
Member

This should now be fixed in the latest release https://github.com/sanity-io/sanity/releases/tag/v3.56.0

@sgulseth
Copy link
Member

Closing for now, feel free to reopen if this is still an issue

Copy link
Contributor

This thread has been automatically locked because it has not had recent activity. Please open a new issue for related bugs and link to relevant comments in this thread.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug typegen Issues related to TypeScript types generation
Projects
None yet
4 participants