This project is read-only.

Support for constants

Topics: Language Specification
Dec 10, 2013 at 8:52 PM

As for now the typescript compiler does not allow defining constants in code but it is possible in plain JavaScript:
const name = 3;
It is supported by modern browsers and could be turned into simple "var"s by typescript compiler for ES3 target (with checking that the constant is never assigned again).

Details here:

What do you think about it?
Dec 12, 2013 at 4:52 PM
As we add support for ES6 features, we'll no doubt add support for this.

We did debate a bit on adding readonly for 1.0 early on, which would have made this easier to add, though it turns out that 'readonly' was more complex than it first seemed. Making properties readonly meant that the type system would have to traffic that information around properly, and if we ever implied readonly-ness during type inference, then the user would have to understand how it worked. Long story short, we decided to hold off until we were going to do a more complete alignment and address it more holistically with the other ES6 features.
Dec 12, 2013 at 7:48 PM
Thanks for the info.

Reading about the readonly properties reminded me about another thing. Have you thought about adding "nullable/non-null" concept to the type system? It seems to be similar to the readonly feature. Or something a lot more powerful like code contracts. Of course I'm well aware of the complexity of those features but I was just curious if you had something like that in mind for future (post 1.0) TypeScript.