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

Inference issue in schemas with nested pipe() #3873

Open
vishin-pavel opened this issue Nov 26, 2024 · 0 comments
Open

Inference issue in schemas with nested pipe() #3873

vishin-pavel opened this issue Nov 26, 2024 · 0 comments

Comments

@vishin-pavel
Copy link

Let's say I want something always to be an array.

const forcedToBeArray = <T>(zodType: ZodType<T>) => {
  const originalType = z.union([zodType, z.array(zodType)]);

  const processedType = z.array(zodType);

  return (
    originalType
      .transform((v) => (!Array.isArray(v) ? [v] : v))
      .pipe(processedType)
  );
};

const expectedTypeScheme = forcedToBeArray(
  z.object({
    t: z.string(),
  })
);

type ExpectedType = z.infer<typeof expectedTypeScheme>; // { t: string }[]

So far, so good. But nested pipe() will be inferred as an originalType instead of processedType

const unexpectedTypeScheme = forcedToBeArray(
  z.object({
    t: forcedToBeArray(z.string()),
  })
);
type UnExpectedType = z.infer<typeof unexpectedTypeScheme>; // { t: string | string[] }[]

I would expect UnExpectedType to be { t: string[] }[]

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