This project is read-only.

"let" and "const" support

Topics: Language Specification
Oct 2, 2012 at 3:56 AM

As Anders explains in the video on Channel 9, TypeScript seems to be trying to preemptively support some of ECMAScript 6's features (C#-style lambdas, classes, etc.). Are there any plans to support "let" (and thus enforce block scoping) or "const" (to declare constants)? These are both nice features that seem like they could be supported in TypeScript with a relatively small footprint.

Furthermore, on the subject of a "const", it seems like this a great area where the compiler could optionally perform an optimization by replacing all the constants with their actual values. Perhaps a compilation flag?

Oct 2, 2012 at 2:18 PM

TypeScript aims to generate simple and clean JavaScript code which can run in ECMAScript3 compatible environments.  One of the challenges with supporting block-scoped let/const in ES3/ES5 environments is that they can require a thicker layer of code generation to run in existing JavaScript engines with correct semantics.  For example, let inside a loop is allocated fresh per loop body, which requires generating an additional closure to model on ES3/ES5.  Also, name shadowing in nested scopes is legal with let, but would require either renaming (which we avoid so far in TypeScript) or more additional closure generation. 

We expect to ultimately add support for ES6 features like 'let' and 'const' at the very least targeting ES6 runtime environments in the future.  However, due to the code generation (and therefore performance predictability, debuggability, etc.), it's not as clear yet that we'll be able to introduce let/const support targeting ES3/ES5.


Oct 6, 2012 at 5:53 PM

I understand the need to ensure for compatibility for existing (and future JavaScript), but couldn't a typed 'var' be subject to 'use before set' checking?


     x = 10;
     var x : number;  // illegal

This way you know that the developer is using TypeScript and is not using regular JS.  For ES6 the typed var declaration could be generated as a let statement.


Oct 7, 2012 at 9:36 AM

We also use let and const in our server-side and XUL application based on Mozilla's SpiderMonkey. SpiderMonkey supports let and const.

So it would be nice if TypeScript could at least just leave these keywords in the resulting JavaScript and handle it internally like var.

Nov 4, 2012 at 12:22 AM


Also working with Mozilla code. For me it would be enough to have a command-line option that turns off errors for "let" and "const", so that i can compile with VS

Dec 18, 2012 at 12:56 PM

TypeScript's compiler (tsc) targets ES3 and ES5.

The const keyword was introduced with JavaScript 1.5 (2000).  TypeScript does not target JS1.5 (2000) through JS1.8.6 (present).

My expectation is that TypeScript will target ES6 sometime after ES6 becomes an approved standard.  Assuming ES6 includes const and let <> as anticipated.

I don't know what the expectation is for TypeScript to retcon those ES6 features for ES3 and ES5 targets.  Or if the compiler will error for ES6-isms that are not retcon'd for ES3 and ES5; or silently fallback to just var.

Dec 18, 2012 at 1:48 PM

It seems unlikely they would silently fall back to var in the case of let, as the two keywords have different scoping rules.