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.

Unions and enums from an array

See original GitHub issue

I might be missing something, but it seems this is currently not possible, but would be a great time/code saver:

const STATUSES = ['Assigned', 'In Progress', 'On Location', 'Succeeded', 'Failed'];
const StatusSchema = z.union(STATUSES.map(z.literal));
const StatusSchema2 = z.enum(STATUSES);

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
ivosabevcommented, Jun 21, 2020

With as const makes it readonly, but won’t work either.

const fishTypes = ['Salmon', 'Tuna', 'Trout'] as const;
const FishEnum = z.enum(fishTypes);
Argument of type 'readonly ["Salmon", "Tuna", "Trout"]' is not assignable to parameter of type '[string, ...string[]]'.
  The type 'readonly ["Salmon", "Tuna", "Trout"]' is 'readonly' and cannot be assigned to the mutable type '[string, ...string[]]'.

Destructuring it works:

const fishTypes = ['Salmon', 'Tuna', 'Trout'] as const;
const FishEnum = z.enum([...fishTypes]);

Maybe the internals of z.enum and z.union could be touched to allow such cases?

0reactions
dvargas92495commented, Jun 28, 2022

hey @colinhacks I’m running into something similar here.

I have the following constant array:

const WORK_TYPES = [
  { id: 1, name: "Task" },
  { id: 2, name: "Project" },
  { id: 3, name: "Initiative" },
] as const;

I’d like to create an enum type that is a union of the name fields of that array. Here’s what I tried doing:

const workType = z.enum(WORK_TYPES.map((w) => w.name));
// Typescript: Source provides no match for required element at position 0 in target

What I think is happening is: .map produces a <union of strings>[], which is technically not guaranteed to have a first element, despite WORK_TYPES being a constant. but the type of enum requires at least one element (which makes intuitive sense).

This theory is supported by the following working:

const workType = z.enum(
        [WORK_TYPES[0].name, ...WORK_TYPES.slice(1).map((w) => w.name)]
);

Any thoughts on a better workaround instead of ^? Or is this an issue with the enum input type?

Read more comments on GitHub >

github_iconTop Results From Across the Web

STRUCT, UNION AND ENUM
A union is a type of structure that can be used where the amount of memory used is a key factor. Similarly to...
Read more >
Union and Enum in C++ - Scaler Topics
Enums consist of integral constants values; by default, they have value as array indexes. Enums are of two types scoped and unscoped. Scoped ......
Read more >
Structures, Unions and Enumerations in C++ - GeeksforGeeks
Structures are used to combine different types of data types, just like an array is used to combine the same type of data...
Read more >
The Difference Between TypeScript Unions, Enums, and Objects
Unlike enums, values in a union represent a set, which is a collection of unique values. So, if any values are repeated, then...
Read more >
Structure - Bryn Mawr College
Structures, Unions, and. Enumerations ... Array and pointer types cannot be defined as macros. ... As with structures and unions, to name an...
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