-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Spread keyof expression #15759
Comments
looks like a duplicate of #13542 |
Yes, true, although I don't quite see how string enums (proposed as the answer in that issue) would work to solve any problem this would solve unless we were to get automatic string enums with the member names of every interface/type somehow. |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
@mhegazy Please reopen. I feel that this could bring a unique capability that string enums could not that may have been overlooked. Instead of having
Update - I noticed I don't need |
I think |
Proposal
I propose an expression that would help bridge the type erasure gap by emitting an array of member names.
The following:
would generate
The elements included in the array by
...keyof(X)
would be exactly every element considered bykeyof X
in a type declaration; not necessarily in any particular order.Discussion
Encasing the
...keyof(*)
expression in brackets (as may be expected from a spread operation meant to provide an array) would make it syntactically ambiguous and prevent someone from having akeyof
function. As far as I know, spread is not used outside of brackets and braces aside from in parameters and left side of assignment, where it would be nonsensical anyway, so at least for now this is probably not syntactically ambiguous with ES.This brings the
keyof()
expression into code space instead of type declaration space, but I couldn't think of a cleaner alternative.There's an argument that it should generate an object with the keys being keys and the types being values; however, it is an enormous can of worms to define which objects should be used to reify the type declarations, or to which extent that should even occur or be allowed. I'm not proposing full introspection, just a way to bridge the gap and carry some remnant of interfaces and types into the generated JavaScript.
The text was updated successfully, but these errors were encountered: