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

discriminatedUnion with recursive type results in ts error #1384

Closed
namjul opened this issue Sep 6, 2022 · 3 comments
Closed

discriminatedUnion with recursive type results in ts error #1384

namjul opened this issue Sep 6, 2022 · 3 comments
Labels
stale No activity in last 60 days

Comments

@namjul
Copy link

namjul commented Sep 6, 2022

I have a situation where I want to use a recursive type inside discriminated union type.

It results into this error:

 Argument of type '[ZodObject<{ type: ZodLiteral<"a">; a: ZodString; }, "strip", ZodTypeAny, { type: "a"; a: string; }, { type: "a"
 ; a: string; }>, ZodObject<{ type: ZodLiteral<...>; b: ZodString; }, "strip", ZodTypeAny, { ...; }, { ...; }>, ZodType<...>]' is n
 ot assignable to parameter of type '[ZodDiscriminatedUnionOption<"type", Primitive>, ZodDiscriminatedUnionOption<"type", Primitive
 >, ...ZodDiscriminatedUnionOption<...>[]]'.
   Type at position 2 in source is not compatible with type at position 2 in target.
     Type 'ZodType<Category, ZodTypeDef, Category>' is missing the following properties from type 'ZodObject<{ type: ZodLiteral<Pri
 mitive>; } & ZodRawShape, any, any, { [x: string]: any; }, { [x: string]: any; }>': _cached, _getCached, shape, strict, and 14 mor
 e.

There seems to be a conflict between ZodType and ZodDiscriminatedUnionOption.

Here the example I used:

interface Category {
  type: "c";
  c: Category[];
}

// cast to z.ZodType<Category>
const category: z.ZodType<Category> = z.lazy(() =>
  z.object({
    type: z.literal("c"),
    c: z.array(category),
  })
);

const item = z
  .discriminatedUnion("type", [
    z.object({ type: z.literal("a"), a: z.string() }),
    z.object({ type: z.literal("b"), b: z.string() }),
    category,
  ])
  .parse({ type: "a", a: "abc" });
@itsgiacoliketaco
Copy link

Hi there, see my suggestion in #1504 for a solution here.

@roblabat
Copy link
Contributor

roblabat commented Oct 28, 2022

I've got this openned PR #1290 fixing this issue and waiting for feed back or merge.

@stale
Copy link

stale bot commented Jan 26, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale No activity in last 60 days label Jan 26, 2023
@stale stale bot closed this as completed Feb 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale No activity in last 60 days
Projects
None yet
Development

No branches or pull requests

3 participants