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

Default enum generic #3439

Closed
codenothing opened this issue Apr 25, 2024 · 0 comments
Closed

Default enum generic #3439

codenothing opened this issue Apr 25, 2024 · 0 comments

Comments

@codenothing
Copy link

If you have existing string union types, enum requires both generics to be defined in order to type guard against the input. Example:

import { z } from 'zod';

type TColors = 'Red' | 'Green' | 'Blue';

// Type error
z.enum<TColors>(["Red", "Green", "Blue"]);

// Success
z.enum<TColors, [TColors, ...TColors[]]>(["Red", "Green", "Blue"]);

To get around this, the createZodEnum overrides need a default generic for the second parameter

function createZodEnum<U extends string, T extends Readonly<[U, ...U[]]> = Readonly<[U, ...U[]]>>(
  values: T,
  params?: RawCreateParams
): ZodEnum<Writeable<T>>;
function createZodEnum<U extends string, T extends [U, ...U[]] = [U, ...U[]]>(
  values: T,
  params?: RawCreateParams
): ZodEnum<T>;
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

No branches or pull requests

2 participants