Replies: 1 comment
-
|
Another attempt: export function getJoinedEntityParser<
OwnShape extends z.ZodRawShape,
RefsShape extends z.ZodRawShape,
OwnParser extends z.ZodObject<OwnShape>,
RefsParser extends z.ZodObject<RefsShape>,
>(
ownParser: OwnParser,
refsParser: RefsParser,
) {
// The result is the same if I use z.objectUtil.MergeShapes below
type JoinedShape = z.extendShape<OwnShape, RefsParser['_shape']> // I'd use simply 'RefsShape', but TS complains
type JoinedParser = z.ZodType<
z.baseObjectOutputType<JoinedShape>,
z.ZodObjectDef<JoinedShape, OwnParser['_unknownKeys'], OwnParser['_catchall']>,
z.baseObjectInputType<JoinedShape>
>
const parser = z.lazy(() => ownParser.merge(refsParser)) as unknown as JoinedParser
return parser
}Then this is what I get when I try to use it: export const entityOneJoinedParser = getJoinedEntityParser(
entityOneOnlyParser,
z.object({
entityTwo: entityTwoJoinedParser,
}),
)
// then I try to extract the type
export type EntityOneJoined = z.output<typeof entityOneJoinedParser>
// this is what VSCode shows when I hover the type
type EntityOneJoined = {
[x: string]: any;
[x: number]: any;
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm having a problem related to #558. I'm trying to use recursive types with
z.lazybut with generic functions of my own.Basically I want to create a Zod parser to a GraphQL schema, and validate all the relations that the GraphQL schema can return. It's objects all the way down...
I successfully created a generic function that returns the accurate type for the object without subobjects. But whatever I type, including forcibly casting the result, something like this:
When I call this, with the following:
Then VSCode only says this when I hover the type (using
z.objectUtil.MergeShapes):And this is what it says when I try
z.extendShapeinstead:I want to get it to say:
Is this possible?
Beta Was this translation helpful? Give feedback.
All reactions