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

Conversion of z.nativeEnum() with negative number fails on TS v5.4 #76

Open
j-murata opened this issue Mar 9, 2024 · 0 comments
Open

Comments

@j-murata
Copy link

j-murata commented Mar 9, 2024

index.mjs
import { z } from 'zod'
import { printNode, withGetType, zodToTs } from 'zod-to-ts'

const $enum = withGetType(z.nativeEnum({
    NEG_1: -1,
    POS_1: +1,
}), ts => ts.factory.createIdentifier('Enum'));

const { store } = zodToTs($enum, 'Enum', { nativeEnums: 'resolve' });
const node = store.nativeEnums[0];

console.log(printNode(node));

When I ran the above script with TypeScript v5.4, I got the following error.

Note

This error did not occur on TypeScript v5.3.

$ node index.mjs
C:\path\to\cwd\node_modules\typescript\lib\typescript.js:21074
      Debug.assert(text.charCodeAt(0) !== 45 /* minus */, "Negative numbers should be created in combination with createPrefixUnaryExpression");
            ^

Error: Debug Failure. False expression: Negative numbers should be created in combination with createPrefixUnaryExpression
    at Object.createNumericLiteral (C:\path\to\cwd\node_modules\typescript\lib\typescript.js:21074:13)
    at file:///path/to/cwd/node_modules/zod-to-ts/dist/index.js:198:58
    at Array.map (<anonymous>)
    at zodToTsNode (file:///path/to/cwd/node_modules/zod-to-ts/dist/index.js:197:61)
    at zodToTs (file:///path/to/cwd/node_modules/zod-to-ts/dist/index.js:67:16)
    at file:///path/to/cwd/index.mjs:9:19
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at async loadESM (node:internal/process/esm_loader:28:7)
    at async handleMainPromise (node:internal/modules/run_main:113:12)

Node.js v20.11.1

diff of package.json
   "dependencies": {
-    "typescript": "5.3",
+    "typescript": "5.4",
     "zod": "^3.22.4",
     "zod-to-ts": "^1.2.0"
   },

Is there any workaround other than continuing to use the old TypeScript?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant