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 not generating union type for string array with list option #6623

Open
oyvind-stenhaug opened this issue May 10, 2024 · 0 comments
Open
Labels
typegen Issues related to TypeScript types generation

Comments

@oyvind-stenhaug
Copy link

oyvind-stenhaug commented May 10, 2024

Describe the bug

If I define a string field with a list of predefined values, TypeGen generates a union type for that field. However, if I define an array field with a list of predefined values (to get checkboxes in the studio), TypeGen just generates Array<string> as the type of that field.

To Reproduce

Steps to reproduce the behavior:

  1. Create a project (opting in to TypeScript) or edit an existing one, and make a sanity.config.ts that's something like this:
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: 'category',
            type: 'string',
            options: {
              list: ['physics', 'chemistry'],
            },
          }),
          defineField({
            name: 'element',
            type: 'array',
            of: [{type: 'string'}],
            options: {
              list: ['physics', 'chemistry'],
            },
          }),
        ],
      }),
    ],
  },
})
  1. npx sanity schema extract && npx sanity typegen generate and observe the generated types (e.g. sanity.types.ts)

Expected behavior

export type Test = {
  _id: string
  _type: 'test'
  _createdAt: string
  _updatedAt: string
  _rev: string
  category?: 'physics' | 'chemistry'
  element?: Array<'physics' | 'chemistry'>
}

Actual behavior

export type Test = {
  _id: string
  _type: 'test'
  _createdAt: string
  _updatedAt: string
  _rev: string
  category?: 'physics' | 'chemistry'
  element?: Array<string>
}

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

@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 21, 2024
@sgulseth sgulseth added bug typegen Issues related to TypeScript types generation labels May 22, 2024 — with Linear
@sgulseth sgulseth removed the bug label May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typegen Issues related to TypeScript types generation
Projects
None yet
Development

No branches or pull requests

4 participants