-
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
Shorter dot syntax to access enum values #45956
Comments
Given that this is not valid JavaScript I really don't see how this would be possible without emitting different JS. Care to share? |
@MartinJohns You're right. Updated |
This is also
Requests of this sort tend to get declined. Usually they'd say "if you want to see this, go to tc39 and get them to put it in JavaScript first". But you're talking about |
My suspicion is that However, for this issue in particular, there’s another reason it’ll almost certainly be declined: it requires type-directed emit. |
As |
This would also be against the design goals of TypeScript:
|
@MartinJohns This is not a breaking change. Any current code will compile without errors after this feature. This is a minor change. Reinforcing: We're talking about |
Removing the |
Ah, sorry. It is true |
@MartinJohns In fact, you argument in this comment makes a lot of sense: #45968 (comment) But, as i said, |
Except we can't because it requires type-directed emit, as noted in my comment above. See Non-Goals here: Specifically # 5 on that list:
|
The TS: enum A {
CASE1 = 'case1'
}
const a = A.CASE1 JS: "use strict";
var A;
(function (A) {
A["CASE1"] = "case1";
})(A || (A = {}));
const a = A.CASE1; As const enum A {
CASE1 = 'case1'
}
const a = A.CASE1 JS: "use strict";
const a = "case1" /* CASE1 */; That is, it emits different code based on the results of the type system I don't see sense in not be able to emit different code based on the results of the type system with a new syntax for a type that emits different code based on the results of the type system |
The snippets above are generated by typescript: https://www.typescriptlang.org/play?#code/KYOwrgtgBAglDeBYAUFKBhGBlAogRigF4oByAYwEMBnYPElAXxRTIHsQqAXKCo2AOky48zZGw7dQkKACEEKNEPx9y1WvWRNkLdlygAjPjMHZ8KIA |
I think this illustrates my point better: shorter-dot-syntax-outlaw.mp4 |
FWIW I love this feature of Swift, but yeah, sadly it’s a no-go for us. I have thought about trying to make completions let you type something like you suggested |
Perhaps this analogy will help:
|
Suggestion
My suggestion meets these guidelines:
⭐ Suggestion
Allow access to
enum
values without having to type the entire path to the value. Any subpath starting with dot is valid.With unions, we have a compiler error when the subpath is ambiguous.
📃 Motivating Example
Sometimes, the path is too long. I have this code in react:
Besides having to import
ATOM
, the path is too long. That way it would be better:This example would emit an
import
ofATOM
at runtime.The text was updated successfully, but these errors were encountered: