Skip to content

TypeGen not generating union type for string array with list option #25

@oyvind-stenhaug

Description

@oyvind-stenhaug

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

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