-
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
Suggestion: Support ES6 'let' and 'const' keyword #19
Comments
What are the type system implications? |
I would state that there's two sides to this:
Personally I like the idea of having const and readonly as an option. It may make things more complicated, but I like that it is explicitly declaring intent. Also, currently, it isn't possible to ensure an initialized value before the constructor is called. |
👍 |
I've inherited a very large firefox add-on, written largely in javascript, that uses const and let all over the place. I'm trying to use typescript to debug, annotate, and refactor this legacy code. It seems a shame to remove "const", and I'm currently unsure how to remove "let". What's the status of including these features? I'm confused by the move from codeplex to github, the threads seem to dissipate. How can I help? I really need this add-on to work right. |
Can you share what add on it is? We've been prioritizing ES6 features for which we can generate ES5 compatible code under the assumption that there's more value there until ES6 support is more prevalent in browsers (and thus more prevalent in JavaScript apps). |
My copy is at https://github.com/wagle/tagspace |
Apparently const and let have been in mozilla's internal javascript forever. |
Newer browsers do tend to support let/const (http://kangax.github.io/compat-table/es6/) but as you can see it's by no means ubiquitous yet (ex iOS8 has no support for let, nor does IE10). We don't expect JavaScript developers en masse to take a dependency on those features until they're more common. Given that, we've been focusing our near term efforts on the ES6 features which we can give people the ability to leverage whether or not the browser supports them yet (ie can emit ES5 compatible code that models the ES6 semantics). That said, ES6 specific features like let/const are certainly important to us. |
Yeah, I guess I need something like "--target js2", and no-one seems anxious to implement the mozilla javascript target. In theory, I'm capable of adding const and let to your transpiler (5 semesters of compilers, years ago), but I'm not yet a "Javascript language lawyer", so I'm uncomfortable. I'm desperate to get this plugin debugged and extended this fall, so it looks like I'm going to try adding these keywords. Adding const seems moderately straightforward, just look for assignments in the scope. I'm not entirely sure what exactly let does yet (give me a day or two), but it just seems to limit the scope of what might be a var even more (correct me if I'm wrong). (1) Would you need me to translate these to ES5 (or all the targets), or can I just disallow it? (2) I might find that my plugin uses more than const and let, so I'd need those to. Would you expect me to implement all of "js2"? Or do I fork because I didn't do it all? (3) Oh. Is the intention for there to be only one source language (typescript), but multiple targets (dialects of javascript)? |
The way we would approach this is adding a new ES6 target. let and const will only work in ES6 but disallowed in other targets. An example to follow would be get and set accessors under ES3. TypeScript is intended to be a super set of JavaScript, so the first thing to do here would be to go through the ES6 spec for let and const (http://wiki.ecmascript.org/doku.php?id=harmony:specification_drafts). Let and const provide block scoping, const adds an additional constraint of being readonly. If there are other ES6-specific features that you need let's take them on a case-per-case basis. let me know if you have other questions. |
@wagle I have initial support for let and const,(https://github.com/Microsoft/TypeScript/tree/letAndConst) I am still missing some checks to disable emit if any of the let and const errors were encountered, but it should be usable. Give it a try and let me know how it fares. |
Problem is that I’m using firefox’s javascript for extensions/add-ons. I’ve slurped my add-on into webstorm, and it seems to turn out that it is unhappy with some of the original javascript. It will take me a couple days to make webstorm happy (I have a backlog of stuff to do), and then I will see what makes typescript+letAndConst unhappy. Thanks!= |
change merged into master #904 |
Even if Typescript is a superset of ES6 and ES5, that doesn`t prevent it from checking assignments to a constant variable, does it? Even if it compiles to ES5 target, it can still check for this in TS and compile away the const when targetting ES5. Am I oversimplifying or what? const and preprocessor defines (#ifdef DEBUG) seem to be simple features to add, I want them. I'm coming from C++ :) |
This is something we are considering. There are problems imposed by the scoping of const and let; let and const are block scoped, meaning that in a loop every iteration has a fresh variable. You can not emulate this using var semantics, without introducing functions to create new scopes. While this is doable it can complicate the generated code and can have adverse perf impact. We have always tried to emit idiomatic JavaScript that would match a human-authored code. |
The case where you actually need a function to create a new scopes are pretty rare, could we not just error on those case ? I find it pretty domageable to not having let/const just for few cases. |
I'm somewhat surprised by this statement and would like to see some data either way (in terms of use of |
From what I understand the only case where you need to emit a closure is when you capture the declared variable in something like a closure:
Any other case can be solved with variable renaming and compiler check, sure it's an handy case, but there is a lot of other case that are valuable and not being able to take advantage of this feature just for this case seems unfortunate. |
I think a big part of what @RyanCavanaugh meant is that |
@DanielRosenwasser While I agree, I still would like to see all other features being transpiled to es5/3, maybe should I create a new issue ? |
https://people.mozilla.org/~jorendorff/es6-draft.html#sec-let-and-const-declarations
The only major implication here how we would treat
export const x = 4;
in the context of an internal module, as this basically introduces the concept of areadonly
variable. See #12The text was updated successfully, but these errors were encountered: