Generate union from options array
See original GitHub issueHello. ๐
How do I validate a Union with a large quantity?
const fruitsMap = [
{ uid: 1, name: "banana" },
{ uid: 2, name: "apple" },
// ... many many many
] as const;
const arrowUIds = fruitsMap.map((v) => v.uid);
type FruitsUidUnions = typeof arrowUIds[number];
type Entity = {
uid: FruitsUidUnions;
// ... many many many
foo: string;
bar: string;
};
const schema: z.ZodSchema<Entity> = z.object({
// It's a lot of work.
uid: z.union([z.literal(1), z.literal(2)]),
});
const schema2: z.ZodSchema<Entity> = z.object({
// Type Error
uid: z.union(arrowUIds),
foo: z.string(),
bar: z.string()
});
const schema3: z.ZodSchema<Entity> = z.object({
// Type Error
uid: z.union(arrowUIds as [z.ZodAny, z.ZodAny, ...z.ZodAny[]]),
foo: z.string(),
bar: z.string()
});
const isValidUid = (p: unknown): p is FruitsUidUnions =>
arrowUIds.some((v) => p === v);
const schema4: z.ZodSchema<Entity> = z.object({
// How about something like this?
uid: z.validate().refine(isValidUid),
foo: z.string(),
bar: z.string()
});
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
String Union to string Array - typescript - Stack Overflow
Method for transforming string union into a non-duplicating array. Using keyof we can transform union into an array of keys of an object....
Read more >2 ways to create a Union from an Array in Typescript
First way to create a union from an arrayโโ It turns out that using typeof keys[number] on any array will force Typescript to...
Read more >Learn TypeScript: Union Types Cheatsheet - Codecademy
TypeScript lets you create a union type that is a composite of selected types ... TypeScript allows you to declare a union of...
Read more >Handbook - Unions and Intersection Types - TypeScript
How to use unions and intersection types in TypeScript. ... for tools which let you compose or combine existing types instead of creating...
Read more >Is it possible to create an array union types as values? - Reddit
If you want to enumerate over a union type, you're best off starting with the array and creating the type from that. This...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi! Thanks for this very fun types challenge!
The problem is about arrays vs tuple, this is a solution to solve this:
This was fun to craft and should work! ๐
This said, my personnal solution for this kind of problem would be to generate the โlot of workโ solution from a source.
As example, I have this script to extract
z.enum
from OpenAPI specs:This should be way easier from a simple JSON (OpenAPI is a bit verbose ๐ )
Or if you have some types, you can also use ts-to-zod (but no
typeof
support (yet))I hope one of the solutions will help you solve your problem. Have fun!
@fabien0102 that truly is some next-level TypeScript wizardry! ๐
I found a more abbreviated approach that probably makes a little more intuitive sense:
Very tricky problem, took me a few tries to get this right. And I had to learn about the
-readonly
syntax which Iโd never seen before. Good issue @baronTommy!