question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enums should support an omit method

See original GitHub issue

Example of what has to be done right now:

const a = z.nativeEnum( { a: 1, b: 2 } as const )
const b = z.nativeEnum( Remeda.omit( a._def.values, [ 'a' ] ) )

b._def.values.b // works
b._def.values.a
//            ^
// Property 'a' does not exist on type 'Omit<{ readonly a: 1; readonly b: 2; }, "a">'.

I’m not sure how to do it with zod enums actually.

This issue would be asking for native and zod enums though to support an omit method.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
JacobWeisenburgercommented, Feb 25, 2022

Here’s how you do it with zod enums

const foo = z.enum( [ 'a', 'b' ] )

type ExcludeA = Exclude<z.infer<typeof foo>, 'a'>
// type ExcludeA = "b"

const bar = z.enum(
    foo.options.filter( x => x !== 'a' ) as [ ExcludeA, ...ExcludeA[] ]
)

bar.enum.b // works
bar.enum.a
//       ^
// Property 'a' does not exist on type 'Values<["b", ..."b"[]]>'.
1reaction
colinhackscommented, Mar 2, 2022

you’re omitting by value?

The inferred type of this this enum is 1 | 2 so you’d omit a literal 2 to remove that option.

const a = z.nativeEnum( { a: 1, b: 2 } as const )
// 1 | 2

You could also do this:

const a = z.nativeEnum({ a: 1, b: 2 } as const);
const omitted = z.omit(a, z.literal(a.values.b));

AND method helpers too?

Yep that’s what I mean by “add the .omit method on the base ZodType”. This would add the .omit method to all Zod schemas.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I guarantee that my enums definition doesn't change ...
Enums should not be thought of as strings or numbers; they are abstract data types. It should not be possible to "output the...
Read more >
How to make the most of Java enums - Oracle Blogs
Anytime you have a set of known constant values, an enum is a type-safe representation that prevents common problems.
Read more >
Handbook - Enums - TypeScript
Enums allow a developer to define a set of named constants. Using enums can make it easier to document intent, or create a...
Read more >
Enum in Java - DigitalOcean
Enum constructors are always private. We can't create instance of enum using new operator. We can declare abstract methods in java enum, then ......
Read more >
Enumeration types - C# reference - Microsoft Learn
You cannot define a method inside the definition of an enumeration type. To add functionality to an enumeration type, create an extension ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found