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

[zod-nestjs] createZodDto with optional #218

Open
af-lsh opened this issue Aug 16, 2024 · 1 comment
Open

[zod-nestjs] createZodDto with optional #218

af-lsh opened this issue Aug 16, 2024 · 1 comment

Comments

@af-lsh
Copy link

af-lsh commented Aug 16, 2024

Hey, I'm trying to create a DTO with optional properties. Simple setup:

export const testSchema = z.object({
  field: z.string().optional(),
  another: z.string(),
});

export class TestDto extends createZodDto(testSchema) {}

However it fails with:

TS2509: Base constructor return type '{ [k in keyof addQuestionMarks<baseObjectOutputType<{ field: ZodOptional<this>; another: ZodString; }>, any>]: addQuestionMarks<baseObjectOutputType<{ field: ZodOptional<...>; another: ZodString; }>, any>[k]; }' is not an object type or intersection of object types with statically known members.
    27 | });
    28 |
  > 29 | export class TestDto extends createZodDto(testSchema) {}

I feel like this would be very basic and common use case. So I wonder what could be wrong on my side?
I followed the instructions of https://github.com/wahyubucil/nestjs-zod-openapi/ to set things up.

Thanks :)

@af-lsh
Copy link
Author

af-lsh commented Aug 16, 2024

So I just figured out I could use field: z.optional(z.string()) and it works like expected 👍
Maybe someone could still check if the other way is working somehow?

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