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

Enumeration number problem #2243

Open
OnkelTem opened this issue Feb 15, 2025 · 1 comment
Open

Enumeration number problem #2243

OnkelTem opened this issue Feb 15, 2025 · 1 comment

Comments

@OnkelTem
Copy link

OnkelTem commented Feb 15, 2025

For some reason numbers are not allowed in enumerations:

const ordersRequest = types.model({
  paymentPeriod: types.optional(types.array(types.enumeration('PaymentPeriod', [15, 30])), [15]),
})

i.e. for a type like: (15 | 30)[].

As far as I understand, I have to use types.union for this:

const ordersRequest = types.model({
paymentPeriod: types.optional(types.array(types.union(types.literal(15), types.literal(30))), [15]),
})

Can numbers be added to enumerations?

@thegedge
Copy link
Collaborator

I can't see why we shouldn't support that! :)

But it is indeed always forced into a string enumeration now because of this line:

const type = union(...realOptions.map(option => literal("" + option)))

I did some digging and found that it's been that way since its introduction, with no explanation as to why we did that: #263

My gut is saying there's no particular reason why this was done, but we'll have to investigate it.

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