Code Monkey home page Code Monkey logo

Comments (21)

RyanCavanaugh avatar RyanCavanaugh commented on March 29, 2024

What are the type system implications?

from typescript.

electricessence avatar electricessence commented on March 29, 2024

I would state that there's two sides to this:

  1. The target language is JavaScript and without the use of ES5 accessors or something greater than ES5, you're not really getting any benefit here in the end code.
  2. const and readonly in C# do not behave the same from a compile stand point. const should exist statically or in an instance but must be initialized before the constructor can only be set by the definition. Where as readonly must be initialized before the constructor finishes.

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.

from typescript.

basarat avatar basarat commented on March 29, 2024

👍

from typescript.

wagle avatar wagle commented on March 29, 2024

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.

from typescript.

danquirk avatar danquirk commented on March 29, 2024

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).

from typescript.

wagle avatar wagle commented on March 29, 2024

My copy is at https://github.com/wagle/tagspace

from typescript.

wagle avatar wagle commented on March 29, 2024

Apparently const and let have been in mozilla's internal javascript forever.

from typescript.

duncanmak avatar duncanmak commented on March 29, 2024

I think @wagle brings up another good point.

Like I wrote in #595, IMHO it's a quicker path and possibly more immediately useful if there's an additional code-gen path to emit non-ES5 (i.e. ES6, or in @wagle's case, perhaps JS2?) code.

from typescript.

danquirk avatar danquirk commented on March 29, 2024

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.

from typescript.

wagle avatar wagle commented on March 29, 2024

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)?

from typescript.

mhegazy avatar mhegazy commented on March 29, 2024

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.

from typescript.

mhegazy avatar mhegazy commented on March 29, 2024

@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.
The problem however is that browsers vary dramatically in their support for let and const. even ones that do support them do no necessary agree on the semantics. My implementation tries to follow the latest draft for ECMAScript6.

from typescript.

wagle avatar wagle commented on March 29, 2024

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!=

from typescript.

mhegazy avatar mhegazy commented on March 29, 2024

change merged into master #904

from typescript.

binarez avatar binarez commented on March 29, 2024

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++ :)

from typescript.

mhegazy avatar mhegazy commented on March 29, 2024

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.
Having said that, I think there is room for us to support let and const for ES5, through flagging captured variables in loops

from typescript.

fdecampredon avatar fdecampredon commented on March 29, 2024

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.

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.
see: https://github.com/olov/defs/blob/master/loop-closures.md

from typescript.

RyanCavanaugh avatar RyanCavanaugh commented on March 29, 2024

The case where you actually need a function to create a new scopes are pretty rare

I'm somewhat surprised by this statement and would like to see some data either way (in terms of use of let "in the wild"). My impression is that using let in a loop construct to avoid explicit closures is arguably the single most valuable thing let actually does, but I could be wrong. The other cases are nice to have IMO, but loop closures are incredibly convenient.

from typescript.

fdecampredon avatar fdecampredon commented on March 29, 2024

I'm somewhat surprised by this statement and would like to see some data either way (in terms of use of let "in the wild"). My impression is that using let in a loop construct to avoid explicit closures is arguably the single most valuable thing let actually does, but I could be wrong. The other cases are nice to have IMO, but loop closures are incredibly convenient.

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:

var arr= []
for (let i = 0; i < 10; i ++) {
  arr.push(() => i)
}

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.
Especially const is quite useful, and not only for scopes properties.

from typescript.

DanielRosenwasser avatar DanielRosenwasser commented on March 29, 2024

I think a big part of what @RyanCavanaugh meant is that let actually gives you the "intuitive" semantics for these cases, which is why it was ever desirable in the first place.

from typescript.

fdecampredon avatar fdecampredon commented on March 29, 2024

@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 ?

from typescript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.