Closed 052hide closed 1 month ago
Because enum
members can't be numeric. What you're doing is similar to:
// this is invalid
enum e {
10,
20
}
A solution is to transform value into string:
const NUMBER_KEY_ENUM = {
10: '10',
20: '20',
} as const
z.nativeEnum(NUMBER_KEY_ENUM)
A solution is to transform value into string: const NUMBER_KEY_ENUM = { 10: '10', 20: '20', } as const z.nativeEnum(NUMBER_KEY_ENUM)
Thank you for the suggestion.
However, since I am generating my code from an API schema, I am unable to change the value to a string. Do you have any suggestions for handling this scenario without enforcing the value as a string?
Could you provide more info about your scenario?
version: v3.23.8
While using numbers as keys might not be a common pattern, I would expect consistent behavior regardless of the key type. However, it appears that enums with number keys and stringified number keys lead to different results, which seems unintuitive.