enum of numbers
See original GitHub issueThis code fails:
import { z } from "zod";
const Multipliers = [1, 2, 3, 4] as const;
const schema = z.enum(Multipliers);
With error
No overload matches this call.
Overload 1 of 2, '(values: readonly [string, ...string[]]): ZodEnum<[string, ...string[]]>', gave the following error.
Argument of type '[1, 2, 3, 4, 5, 6]' is not assignable to parameter of type 'readonly [string, ...string[]]'.
Type at position 0 in source is not compatible with type at position 0 in target.
Type 'number' is not assignable to type 'string'.
Overload 2 of 2, '(values: [string, ...string[]]): ZodEnum<[string, ...string[]]>', gave the following error.
Argument of type '[1, 2, 3, 4, 5, 6]' is not assignable to parameter of type '[string, ...string[]]'.
Type at position 0 in source is not compatible with type at position 0 in target.
Type 'number' is not assignable to type 'string'.ts(2769)
Is there any reason why z.enum
only expects string
s?
Issue Analytics
- State:
- Created a year ago
- Reactions:6
- Comments:5
Top Results From Across the Web
How can I create an enum using numbers? - Stack Overflow
You can get numbers listed in an enum in the inspector by simply using an underscore prefix, e.g: public enum MyNumbers{ _1, _2,...
Read more >C# Enumerations Type - Enum - TutorialsTeacher
In C#, an enum (or enumeration type) is used to assign constant names to a group of numeric integer values. It makes constant...
Read more >Can we have integers as elements of an enum in Java?
Integers as elements of an enum. No, we can have only strings as elements in an enumeration. Using strings in it, generates a...
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 >Telephone number mapping - Wikipedia
The most prominent facility for telephone number mapping is the E.164 number to URI mapping (ENUM) standard. It uses special DNS record types...
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 FreeTop 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
Top GitHub Comments
@colinhacks Would you be open to a PR to add number support to enums? I know it isn’t a 1:1 with the TypeScript definition of an enum, but this is in practice much more akin to a union (that’s even what it infers to) than it is an enum in the TypeScript sense.
For anyone else running into this problem, this is how I worked around the limitation in
z.enum
:Playground link