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

Unknown node of kind "NewExpression" - When using Set values as union #2054

Open
gotenxds opened this issue Aug 30, 2024 · 3 comments
Open

Comments

@gotenxds
Copy link

This might be a fringe case and I'm going to workaround it using Enums instead but I still think this is intersting and should probably be fixed / we should add a proper error here.

When using a normal union type like so 'a' | 'b' the schema generator works just fine.
But when creating a union type from a Set (it's the same union) we an error.

to create the union first create a helper type like so:
export type SetValues<T> = T extends Set<infer V> ? V : never;

Then call this type with your set (it is important to use as const with the provided array)
type myUnion = SetValues<new Set(['a', 'b'] as const)> // 'a' | 'b'

use this type in an interface to generate a schema

Get following error:

UnknownNodeError: Unknown node of kind "NewExpression" at ChainNodeParser.getNodeParser (C:\project\node_modules\ts-json-schema-generator\src\ChainNodeParser.ts:50:15)

@arthurfiorette
Copy link
Collaborator

Im not sure if the example you provided is a valid TS syntax.

type myUnion = SetValues<new Set(['a', 'b'] as const)>

@gotenxds
Copy link
Author

Hya @arthurfiorette , yes, my bad, you need to extract the set into a variable first.

Here is an example in the playground:

@arthurfiorette
Copy link
Collaborator

Would you like to work on a PR? Remember to add unit tests :)

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

2 participants