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

Compatibility with inferred types from zod #758

Closed
matt-pawley opened this issue Apr 27, 2021 · 3 comments
Closed

Compatibility with inferred types from zod #758

matt-pawley opened this issue Apr 27, 2021 · 3 comments

Comments

@matt-pawley
Copy link

matt-pawley commented Apr 27, 2021

Hi, our codebase for various reasons has a mix of zod types and typescript types.

We are hoping to generate JSON schema from the typescript types (which in turn use inferred zod types). However upon trying we get the following error TypeError: Cannot read property 'types' of undefined which is thrown here

A minimal reproducible example is below:

// foo.ts

import * as zod from 'zod';

const Foo = zod.number();
type FooType = zod.infer<typeof Foo>;

export interface Example {
  foo: FooType;
}

Can be ran with npx ts-json-schema-generator --path ./foo.ts --type Example

Appreciate this is probably out of the remit of this library, but would it be something you would be open to having a PR for? If so, could point me in the right direction to fix ?

@domoritz
Copy link
Member

Thanks for the issue. To debug this, I suggest inlining
The types. Then use the debugger to get a stack trace and see where the error is thrown. Yes, I'm happy about prs!

@zmk-punchbowl
Copy link

zmk-punchbowl commented May 1, 2023

@matt-pawley we're running into a similar issue (using https://github.com/env0/serverless-openapi-typescript). Did you end up finding a way forward?

This is the error we're getting when a type that uses zod infer is encountered. In this case types is the property that is undefined.

TypeError: Cannot read properties of undefined (reading 'map')
    at CallExpressionParser.createType (.../node_modules/ts-json-schema-generator/dist/src/NodeParser/CallExpressionParser.js:25:71)

@timothympace
Copy link
Contributor

timothympace commented May 3, 2024

I also ran into this and found a fix for the problem: #1947

I don't understand the purpose of the FIXME in the first place, but it seems like a good idea to fully validate the type before trying to use methods that might not exist.

@domoritz domoritz closed this as completed May 6, 2024
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

4 participants