-
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
array enumerations as runtime array + union type #48193
Comments
Since the proposed syntax has runtime representation, it violates
Which you checked. The existing |
The intent for non-enum scenarios is really that you write the "... and be identical to..." block. Given an |
The syntax doesn't have runtime representation since the syntax to be added is the
const Status = ['active', 'inactive', 'cancelled'] as const;
type Status = typeof Status[number]; The reason to not do this is "requires users to declare both the runtime array and the type in separated sentences and declare them with the same name", unavoidable any other way. I didn't know about the TC39 enum proposal. Is it this one? because this is Stage 0 with no updates since 2018. This would still will be interesting, though, and wouldn't collide with such definition. |
You're proposing that the expression |
I fail to see why Anyway, would this be better? const X = [ 'foo', 'bar' ] as enum; At the end the runtime variable would be unchanged, we want to create a I was trying to not add new keywords but maybe const X = [ 'foo', 'bar' ] as union; |
Regardless of the choice of keyword, this is expression-level syntax. What do you propose should happen here: foo(['x', 'y'] as enum); If you intend that the creation of a new named type is tied specifically to the |
foo(['x', 'y'] as enum); Yes, this would be invalid syntax. I was thinking of this as a statement rather than an expression.
The reason to not do that was exactly what you say, this would be TypeScript syntax generating runtime code. That's not what this suggestion is about, it's about decorating an already existing, javascript valid, runtime declaration in such a way that TS knows a union type with the same name has to be created with it, any syntax would do as long as the Javascript code is still intact: // as comment
const MyArray = ['a']; // @ts-enum
// or
const MyArray = ['a']; // ts please make this `as const` and union type from it
// As decorator if we could decorate variables
@union
const MyArray = ['a']
// Adding a keyword in what otherwise would be invalid syntax
const MyArray = ['a'] enum;
const MyArray = enum ['a'];
const MyArray enum = ['a'];
// Omited because it can be confused with `const enum`
// enum const MyArray = ['a']; I don't understand why would we say that const MyArray = enum ['a'] has a runtime impact because "is not, in and of itself, valid JS syntax" when the same can be said about each one of the following sentences: const x: number = 1;
function fn(x: number): number { return x }
const x = 1 as const;
function fn(x?) {}
myGenericFunction<string>(); and any other TS syntax that extends JS syntax to add type information. As I see it it's not that the syntax has runtime effect but the syntax decorates runtime code with type information. |
The difference from all those other examples is that here you're proposing to put
https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals |
@fatcerberus are you saying that if it's not part of the expresion side of the const MyArray enum = ['a'];
const union MyArray = ['a'];
I checked that before and as said, it was never expected this to be an expression. |
Hi, I think the repetitive requests on I am not sure whether `Object.defineProperty´ would violate non-goals, like this
would compile to
But it might not be very compatible with early ES. But we can enable this keyword when and only when the target supports Objcet.defineProperty |
This issue has been marked as "Declined" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Suggestion
Array enums
🔍 Search Terms
enum array
array as union
Related tickets
enum
withconst
modifier because of inline optimization #21391✅ Viability Checklist
⭐ Suggestion
The idea is that if
enum
keyword is used in a constant array declaration, it leaves the array as runtime value but also returns the type as a union type of the values.📃 Motivation
Currently
enums
are causing lots of issues by their nature that violates a few of the TypeScript design goals:They require devs to create key-value pairs for each entry in the enum and that transpiles to a considerable amount of runtime code.
const enums
came to help there but they lack the ability to get the list of valid values as a runtime array for runtime validation.This can solve both issues with a terse, clean and removable syntax addition, and facilitates the usage of union types as enumerations.
💻 Use Cases
Would transpile to...
.. and be identical to...
That means that we can use
Status
both as runtime value:And as a type
Even combined
Abstraction in code
This abstraction reaches the same goal but requires users to declare both the runtime array and the type in separated sentences and declare them with the same name.
Comparison with
enum
&const enum
Declaration
Usage as type
Value usage
Enumerate possible values at runtime
Generated output
AlternativeAlternatively we can extend closer to theas const
feature:Discarded since
as const
is expression syntax (see comments)The text was updated successfully, but these errors were encountered: