Unions and enums from an array
See original GitHub issueI 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:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top 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 >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
With
as const
makes itreadonly
, but won’t work either.Destructuring it works:
Maybe the internals of
z.enum
andz.union
could be touched to allow such cases?hey @colinhacks I’m running into something similar here.
I have the following constant array:
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:What I think is happening is:
.map
produces a<union of strings>[]
, which is technically not guaranteed to have a first element, despiteWORK_TYPES
being a constant. but the type ofenum
requires at least one element (which makes intuitive sense).This theory is supported by the following working:
Any thoughts on a better workaround instead of ^? Or is this an issue with the
enum
input type?