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

Enhance ZodArray .min() and .length() Types #2656

Closed
wants to merge 5 commits into from

Conversation

samchungy
Copy link
Contributor

@samchungy samchungy commented Aug 15, 2023

Resolves #2658

@netlify
Copy link

netlify bot commented Aug 15, 2023

Deploy Preview for guileless-rolypoly-866f8a ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 68771b9
🔍 Latest deploy log https://app.netlify.com/sites/guileless-rolypoly-866f8a/deploys/64dc183bb71eaa0008abbc3a
😎 Deploy Preview https://deploy-preview-2656--guileless-rolypoly-866f8a.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@samchungy samchungy changed the title Upgrade Array Types Enhance Array Types Aug 15, 2023
@samchungy samchungy marked this pull request as ready for review August 15, 2023 14:43
@samchungy samchungy changed the title Enhance Array Types Enhance ZodArray .min() and .length() Types Aug 15, 2023
@colinhacks
Copy link
Owner

I dig the concept but this particular incarnation won't fly. That recursive alias can really hurt performance under the best of circumstances, and I'm sure there are many usages of z.number().length(10000) in the wild.

Since this ultimately resolves to a tuple type I'd be more amenable to keeping separate from ZodArray. (In retrospect even .nonempty() should probably have returned a ZodTuple).

Maybe a .tuple() method on ZodType?

z.string().tuple(3);
// ZodTuple<[ZodString, ZodString, ZodString]>

@samchungy
Copy link
Contributor Author

Closing to rethink

@samchungy samchungy closed this Aug 22, 2023
@damian-balas
Copy link

@samchungy @colinhacks Any progress on this?

I need this type

type TypeINeed = [number, number];
type TypeItProduces = [number, ...number[]];

I've used this methonds, but none returned infered type as: [number,number]

{
  rooms: z.array(z.number()).nonempty().min(2).max(2).length(2),
  rooms: z.number().array().min(2).nonempty(),
  rooms: z.number().array().min(2).max(2).nonempty(),
}

Is there a way in the current version?

@samchungy
Copy link
Contributor Author

@samchungy @colinhacks Any progress on this?

I need this type

type TypeINeed = [number, number];
type TypeItProduces = [number, ...number[]];

I've used this methonds, but none returned infered type as: [number,number]

{
  rooms: z.array(z.number()).nonempty().min(2).max(2).length(2),
  rooms: z.number().array().min(2).nonempty(),
  rooms: z.number().array().min(2).max(2).nonempty(),
}

Is there a way in the current version?

Sounds like you're after a tuple rather than an array

@dca123
Copy link

dca123 commented Aug 13, 2024

I've a usecase where I'm using length to ensure that I only have 8 elements in the array. Figured i should use tupples instead. However, I don't wanna wanna write those elements, 8 times in the array in my code (would be quite verbose) so i built this lil helper function to do that for me.

type TimesTuple<
  N extends number,
  T,
  R extends Array<T> = [],
> = R['length'] extends N ? R : TimesTuple<N, T, [...R, T]>;

function times<N extends number, T>(n: N, obj: T): TimesTuple<N, T> {
  const returnArr = [];
  for (let i = 0; i < n; i++) {
    returnArr.push(obj);
  }
  return returnArr as TimesTuple<N, T>;
}

would it make sense to have an optional param as the second parameter to have this repeating feature ? wonder how people commonly use the tuples feature 🤔

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

Successfully merging this pull request may close these issues.

Stricter Array Types
4 participants