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

@strict modifier doesn't work with inline types (array) #271

Open
yuriibut opened this issue Oct 7, 2024 · 1 comment
Open

@strict modifier doesn't work with inline types (array) #271

yuriibut opened this issue Oct 7, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@yuriibut
Copy link

yuriibut commented Oct 7, 2024

Bug description

@strict modifier is ignored for inline typed arrays.

P.S: I want to make everything strict by default, so I have to add the modifier everywhere.
It would be great to have config property for make everything strict by default,

Input

export type MyType = {
  /** @strict */
  working: {
    name: string;
  };
  notWorking:
  /** @strict */
  {
    name: string;
  }[];
};

Expected output

export const myTypeSchema = z
  .object({
    working: z
      .object({
        name: z.string(),
      })
      .strict(),
    notWorking: z.array(
      z.object({
        testing: z.string(),
      }).strict(),
    )
  });

Actual output

export const myTypeSchema = z
  .object({
    working: z
      .object({
        name: z.string(),
      })
      .strict(),
    notWorking: z.array(
      z.object({
        testing: z.string(),
      }),
    )
  });

Versions

  • Typescript: v4.9.5
  • Zod: v3.23.8
@tvillaren tvillaren added the bug Something isn't working label Oct 7, 2024
@tvillaren
Copy link
Collaborator

Hello,

Thanks for reporting. Indeed, this does not work as the current version of the code applies the JSDoc tags to the array.
As you probably found out already, a workaround would be to extract the type you want strictly validate:

export type MyType = {
  /** @strict */
  working: {
    name: string;
  };
  notWorking: Working2[];
};

  /** @strict */
type Working2 = {
  name: string;
}

As for having a "strict" option for the whole project, that's a nice idea, not so easy to implement: it would probably need some refactoring.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants