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.

This 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 strings?

https://codesandbox.io/s/zod-number-union-4pqezv

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:6
  • Comments:5

github_iconTop GitHub Comments

1reaction
joshuatcommented, Dec 13, 2022

@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.

0reactions
joshuatcommented, Sep 22, 2022

For anyone else running into this problem, this is how I worked around the limitation in z.enum:

import { z } from 'zod';
import type { ZodType } from 'zod';

export const LIMIT_OPTIONS = [10, 25, 50] as const;

export function numericEnum<TValues extends readonly number[]>(
  values: TValues,
) {
  return z.number().superRefine((val, ctx) => {
    if (!values.includes(val)) {
      ctx.addIssue({
        code: z.ZodIssueCode.custom,
        message: `Invalid enum value. Expected ${values.join(
          ' | ',
        )}, received ${val}`,
      });
    }
  }) as ZodType<TValues[number]>;
}

const TestSchema = z.object({
  property: numericEnum(LIMIT_OPTIONS),
});

export type Test = z.infer<typeof TestSchema>;

Playground link

Read more comments on GitHub >

github_iconTop 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 >

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