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

ZodObject has all properties optional #184

Open
Carl-Foster opened this issue Nov 9, 2024 · 3 comments
Open

ZodObject has all properties optional #184

Carl-Foster opened this issue Nov 9, 2024 · 3 comments

Comments

@Carl-Foster
Copy link

For some reason, all inferred types of ZodObject have the properties as optional. This is particularly annoying when extracting a type from a discriminated union.

const zContentPageNode = z.discriminatedUnion("type", [
  z.object({
    type: z.literal("component"),
    componentVersion: z.string().ulid(),
    content: z.record(z.unknown()),
  }),
  z.object({
    type: z.literal("formattedText"),
    content: z.record(z.unknown()),
  }),
]);

type zContentPageNode = z.infer<typeof zContentPageNode>;
type ComponentNode = Extract<zContentPageNode, { type: "component" }>;
// expect: { type: "component", componentVersion: string, content: Record<string, unknown> }
// actual: never

Without chanfana, this works as expected

TypeScript version 5.6.3
zod 3.23.8
hono 4.6.9
chanfana 2.1.0

@Carl-Foster
Copy link
Author

Nevermind, looks like it was due to my tsconfig not setting strict: true.

@Carl-Foster
Copy link
Author

The reason I noted this here was due to creating the application through the create-cloudflare method. The OpenAPI rest application starter has strict:false

@G4brym G4brym reopened this Nov 10, 2024
@G4brym
Copy link
Member

G4brym commented Nov 10, 2024

I've reopened the issue, to remember to update the create-cloudflare template later
Thanks for finding this @Carl-Foster

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