cloudflare / cloudflare/chanfana

ZodObject has all properties optional

Open
#184 3 comments 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
767
Forks
70
Avg merge
25m
Merged PRs (30d)
4

Description

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.
```ts
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;
type ComponentNode = Extract;
// expect: { type: "component", componentVersion: string, content: Record }
// 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

Contributor guide

Open the contributing guide

Research direction

Reproduce the supplied TypeScript example with chanfana 2.1.0 and compare the inferred ZodObject and discriminated-union types with and without chanfana. Trace the schema/type entry points used by chanfana; done when required properties are preserved and Extract produces the expected ComponentNode type.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
backend-api-design
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.