-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Proposal: strictly-typed enums #53013
Comments
This was discussed a lot during the design of #50528 and ultimately decided against due to the potential for TC39 enums to want to use similar syntax. We're basically "hands off" on |
@RyanCavanaugh thanks for adding that link! I hadn't seen the proposal yet. |
For what it's worth, this is now an error in TS 5.0. |
I'd forgotten about that! |
That's tricky in particular because it'd effectively outlaw flag enums (which selfishly, matters a lot to the TypeScript toolchain internally). |
well, since it's been stage 0 with no updates (not including subtle link fix yesterday) for almost 5 years isn't it pretty safe to say that tc39 is not interested in it? |
There are ongoing discussions among TC39 members about enums |
Unfortunately, the recent change doesn't "fix" the weirdness that |
I think that it's completely okay to outlaw flag enums with new syntax to narrow and strictify enums. |
Suggestion
π Search Terms
enum single type syntax
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
I'd like to propose the addition of additional syntax for enums to help constrain them and also allow some DevX improvements.
I've drawn inspiration from flow's syntax for enums as I feel like it's a minimal addition to the syntax whilst also being clear.
Syntax
Allowed Types
As per the types currently allowed by TS, the allowed
enum-type
values may only bestring
ornumber
. In future one could consider extending this to includesymbol
orboolean
to match Flow, but for now we'd just be looking for parity.When declared with
of string
, all enum members must be strings. Similarly when declared withof number
, all enum members must be numbers.Value Uniqueness
When declared with
of
, all enum members must be declared with a unique value.This constraint also means that referencing other members in the enum is no longer valid, and thus TS need not declare a scope or variables for the enum names.
Defaulted vs Initialised Members
String
When declared with
of string
, any defaulted enum members will have a value equal to the string value of the enum name. Any initialised members will have the value as specified.Number
When specified with
of number
, defaulted members will be set to an integer, starting at 0 and incremented for each member.An enum may declare default or initialised members, though defaulted members may not be used after an initialised member. This constraint exists to make it easier to reason about value uniqueness and ordering.
Nominally Typed
Comparisons and assignments of non-enum values to locations typed with an
of
enum are not allowed.However, enums may be assigned to locations expecting their base types, or may be used in ways afforded by their base types:
π» Use Cases
The big things I'm looking to achieve with this proposal are as follows:
foo = 'A'
orfoo === 'A'
being valid if there's a member with the value'A'
) or even dangerous (egfoo = 99
if there are any number-typed members, even if there are no members with the value99
).The text was updated successfully, but these errors were encountered: