sanity: @sanity/codegen crashes on typegen with "operator undefined" error

Describe the bug When running pnpm typegen, the command fails with a TypeError: Property operator expected type of string but got undefined. This error seems to come from the @sanity/codegen package during the generateKnownTypes step.

To Reproduce Steps to reproduce the behavior:

  1. Run pnpm typegen script, which includes: sanity schema extract --enforce-required-fields --path=./src/sanity/extract.json && sanity typegen generate
  2. Wait for type generation to start
  3. The error appears in the terminal and the process exits with code 1

Expected behavior I expected the type generation to complete successfully and generate correct TypeScript types for my schema.

Error log

$ pnpm typegen

> yusifaliyevpro@1.0.0 typegen C:\Users\yusif\Documents\CodeProjects\YusifAliyevPro
> sanity schema extract --enforce-required-fields  --path=./src/sanity/extract.json && sanity typegen generate && rm ./src/sanity/extract.json


   ┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
   │                                                                                                               │
   │   The `sanity` package is moving to v4 on July 15 and will require Node.js 20+.                               │
   │   Learn what this means for your apps at https://www.sanity.io/blog/a-major-version-bump-for-a-minor-reason   │
   │                                                                                                               │
   └───────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

✓ Extracted schema to ./src/sanity/extract.json with enforced required fields

   ┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
   │                                                                                                               │
   │   The `sanity` package is moving to v4 on July 15 and will require Node.js 20+.                               │
   │   Learn what this means for your apps at https://www.sanity.io/blog/a-major-version-bump-for-a-minor-reason   │
   │                                                                                                               │
   └───────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

⠼ Generating types(node:20232) Warning: Closing file descriptor 3 on garbage collection
(Use `node --trace-warnings ...` to show where the warning was created)
(node:20232) [DEP0137] DeprecationWarning: Closing a FileHandle object on garbage collection is deprecated. Please close FileHandle objects explicitly using FileHandle.prototype.close(). In the future, an error will be thrown if a file descriptor is closed during garbage collection.
⠹ Generating types
TypeError: Property operator expected type of string but got undefined
    at Object.validate (C:/Users/yusif/Documents/CodeProjects/YusifAliyevPro/node_modules/.pnpm/@babel+types@7.28.0/node_modules/@babel/types/lib/definitions/utils.js:146:11)
    at validateInternal (C:/Users/yusif/Documents/CodeProjects/YusifAliyevPro/node_modules/.pnpm/@babel+types@7.28.0/node_modules/@babel/types/lib/validators/validate.js:22:9)
    at Object.tsTypeOperator (C:/Users/yusif/Documents/CodeProjects/YusifAliyevPro/node_modules/.pnpm/@babel+types@7.28.0/node_modules/@babel/types/lib/builders/generated/lowercase.js:2587:3)
    at TypeGenerator.generateKnownTypes (C:/Users/yusif/Documents/CodeProjects/YusifAliyevPro/node_modules/.pnpm/@sanity+codegen@3.96.0/node_modules/@sanity/codegen/lib/index.js:580:39)
    at main (C:/Users/yusif/Documents/CodeProjects/YusifAliyevPro/node_modules/.pnpm/@sanity+cli@3.96.0_@types+n_d9a15370aa3afddaee26a611898bdba7/node_modules/@sanity/cli/lib/workers/typegenGenerate.js:13:186)
⠇ Generating types ELIFECYCLE  Command failed with exit code 1.

Which versions of Sanity are you using?

@sanity/cli: 3.96.0  
@sanity/codegen: 3.96.0

What operating system are you using? Windows 11

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

pnpm: 10.12.4
node: v22.16.0

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 22
  • Comments: 25 (3 by maintainers)

Most upvoted comments

I also run into this issue ~and downgrading also doesn’t fix it~ (correction: Downgrading works as long as package-lock.json does not bump @babel/types package version). After investigating I found that the issue could be coming from a recent change in @babel/types package (version 2.8.0, specifically this PR https://github.com/babel/babel/pull/17009). Downgrading that package fixes the issue for me.

  // package.json
  "overrides": {
    "@babel/types": "7.27.7"
  }

Thanks @phucnguyen035

For pnpm users, use “pnpm.overrides” in root package.json

{
  "name": "root-package.json",
  "packageManager": "pnpm@10.11.0",
  "pnpm": {
    "overrides": {
      "@babel/types": "7.27.7"
    }
  }
}

Looks like it’s been released now. Thanks folks!

https://www.sanity.io/docs/changelog/dafaceb1-4859-44b0-a4eb-0d29e9de07ba

Here’s a minimal reproduction repo for debugging: https://github.com/hex-digital/typegen-error-reproduction

The problem continues to occur no matter how far I downgrade sanity (tested to 3.80.0) (EDIT: this may be a red herring as noted below by others, as it may be I was still getting a later version of @babel/types)

Image

Workaround

As noted by @phucnguyen035 and @largis21 (thanks folks!), adding the below workaround to package.json resolves the issue

  "pnpm": {
    "overrides": {
      "@babel/types": "7.27.7"
    }
  }
Image

I am also facing the same issue. Created a simple project using create-sanity and followed the commands specified in the “Generating types from your schema” course. The schema extraction is seemingly fine, but the error occurs when executing typegen generate.

Thank you for reporting this, a fix is going out today as part of v3.97.0

The following seems to work for Yarn v1 in the root package.json

  "resolutions": {
    "@babel/types": "7.27.7"
  },

Hi, thanks for reporting. We are looking into fixing this, the issue seems to be with @babel/types@7.28 - a mitigation until the fix is released is to pin @babel/types to an earlier version like mentioned above

Try running pnpm sanity or npx sanity. I had the issue when running pnpm dlx sanity that it didn’t pick up the local overrides as it always used what is defined in the global installation.

Of course, Downgrading works, but need to solve by PR. I couldn’t find anything related to this issue on latest changelog. A Sanity developer must solve this issue.

@SkinnyAG Had to downgrade to 3.95 and then clear node modules and package-lock.json to get it working. When it gets installed with legacy peer deps, package lock doesn’t play along well.

Same issue here, it stopped working after upgrading to 3.96! It doesn’t solve the error by downgrading back to 3.95.

Issue also occurs here, can’t get it fixed with downgrading unfortunately.

Upgrading to 3.96.0 for sanity and @sanity/vision led me to this issue. Downgrading works, but can’t deploy to Sanity cloud without upgrading.

[edit: apologies, I realised I can opt out to deploying without upgrading]

for npm users the command is

npm pkg set overrides."@babel/types"="7.27.7"
npm i

then typegen worked 😛

Just bumped into this one. Downgrading worked. Hopefully the team will push a fix soon 🙏

Same problem here, just noticed it after upgrading sanity

The main cause is @sanity/cli i think