Today we’re happy to announce TypeScript 1.4. With TypeScript 1.4, we’ve continued to build new features that help you work with more JavaScript patterns, create richer typings, and use new ES6 features.
You can try these improvements out as part of Visual Studio 2015 CTP5, Visual Studio 2013, NPM, and as source.
Type System Improvements
Union Types
JavaScript functions may take a number of possible argument types. Up to now, we’ve supported this using function overloads. Starting with TypeScript 1.4, we’ve generalized this capability and now allow you to specify that that a value is one of a number of different types using a union type:
function f(x: number | number[]) { if (typeof x === "number") { return x + 10; } else { // return sum of numbers } }
Once you have a value of a union type, you can use a typeof and instanceof checks to use the value in a type-safe way. You’ll notice we use this in the above example and can treat x as a number type inside of the if-block.
Union types are a new kind of type and work any place you specify a type.
Type Aliases
You can now define an alias for a type using the type keyword:
type PrimitiveArray = Array<string|number|boolean>; type MyNumber = number; type NgScope = ng.IScope; type Callback = () => void;
Type aliases are exactly the same as their original types; they are simply alternative names. You can use these aliases to better document your code and aid readability.
Const Enums
For heavy uses of enums, it’s helpful to have an even more restricted form that we know is safe to always inline. This helps with performance, code size, and with working with cases where the enum aliases themselves may not be exported. Starting with TypeScript 1.4, you’ll be able to make const enums.
const enum Color { Blue, Green, Red }; var c = Color.Blue;
When compiled, the const enum is removed, leaving ‘var c = 0 /* Blue */’.
And more…
You can get also more information and examples about updates to the type system on our TypeScript 1.4 sneak peek blog post.
ECMAScript 6 Support
In addition to the type system improvements, one of the main goals for the upcoming TypeScript 2.0 release is to fully support the ECMAScript 6 standard. With TypeScript 1.4, we take another step towards this goal. In this release, we’ve added a new ES6 output mode, support for let and const, and support for ES6 template strings.
ES6 output mode
In previous TypeScript releases, we included a ‘–target’ commandline option that allows you to choose between ES3 and ES5 output modes. We’ve added a new ‘ES6’ option to the targets commandline option:
> tsc --target ES6 myfile.js
This option will be required for ES6 features that cannot be output to ES3 or ES5, just as ES5-only features, such as getters and setters, have required the ES5 target option.
We’re in the process of updating the existing language features from ES6, such as lambdas and classes, to support ES6 mode by outputting their native forms. You may need to install an additional .d.ts file for this to work.
Let/Const
The ES6 ‘let’ feature is similar to ‘var’, but it aims to simplify the mental model for the variable’s scope. With ‘let’, you can scope variables to blocks of code rather than whole functions. For example:
function f() { let total = 0; let x = 5; for (let x = 1; x < 10; x++) { total += x; } console.log(x); } f(); // outputs 5
Notice how the two ‘let x’ statements do not conflict. This is because the one used in the loop is in a different scope than the one outside of the loop. If we re-wrote this using vars, all ‘x’ vars effectively combine into one, leading to rather confusing output.
function f() { var total = 0; var x = 5; for (var x = 1; x < 10; x++) { total += x; } console.log(x); } f(); // outputs 10
TypeScript now supports using ‘let’ and ‘const’ in addition to ‘var’. These currently require the ES6 output mode, but we’re are investigating relaxing this restriction in future versions.
Template Strings
ECMAScript 6 has further improved on string interpolation in JavaScript by adding template strings. These special strings can freely mix in expressions, allowing a lighter syntax when pieces of a string depend on associated values:
var rectangle = { height: 20, width: 10 }; var areaMessage = `Rectangle area is ${rectangle.height * rectangle.width}`;
With the 1.4 release, TypeScript now supports ES6 template strings and can also compile them down to ES3/ES5 expressions.
Looking Ahead
We’re excited to bring new features into TypeScript, including more ECMAScript 6 features and our work on async/await. As always, we’d love to hear your feedback. You can get involved by trying out the release, sending us a bug, playing with the source, and sending us a pull request.

I was facing the annoying circular dependency compiler error literally right now and went looking when 1.4 with the fix will be ready.
And what do I see? It's out and working like a charm again 🙂
Thanks TS team for your work! I went from avoiding JavaScript at all costs to someone who sometimes enjoys writing TypeScript if C# isn't an option.
YaY
Great update guys! Nice features! 🙂
Awesome that template strings work and compile down to regular old ES5. Woohoo!
p.s. My TypeScript web app for homeless youth made it to the Hacker News front page yesterday. See debuggerdotbreak.wordpress.com/…/show-and-tell-beds-and-services-for-homeless-youth
Any chance this update will fix the "Output(s) generated successfully" bug. Where quite often saving a .ts file will not generate the .js/.map file on save, and thus that message is never output to the status bar?
Bug report:
const enum Color { Blue, Green, Red };
var c = Color.Red;
When compiled, the const enum is removed, leaving ‘var c = 0 /* Blue */’.
I think you mean 'var c = 1 /* Red */'
This is awesome! Also Markus same here! TS makes it painless. It would be crazy to not use it!
I'm loving the template strings functionality. One suggestion: Visual Studio should color string templates like it does regular strings.
@Kevin, Can you provide more details about the issue you are running into. it would be great if you can log an issue to track it on the TypeScript github issue tracker: github.com/…/issues
It says on the milestones page that TypeScript 1.4 will expose a public API for the language service.
Did this happen?
Docs?
@Paul Young, you can find API documentation at github.com/…/Using-the-Compiler-API
Nice work. Fantastic improvements.
Having let supported is great but pity its not supported down to es5. Can you explain why you held off on this?
@Dave Hanson
let and const are a tricky to emulate correctly and efficiently in all cases. This is not just additionnal syntax sugar for things you could do before (like string interpolation), it's a change in the language semantics.
If you look at the Traceur transpiler for instance, you'd see that it creates:
try { throw rightHandValue } catch (variable) { … }
to emulate let in ES5. This is ugly and poorly performing code.
Good update guys, thanks.
Is there any thought being given to enabling compilation of multiple TS files into a single commonJS module, with matching combined single d.ts file? This is currently the one major headache for structuring large modular TS apps – we want to package a bunch of related classes as a single commonJS module for consumption by other modules, and creating a single definition file to represent this module is a nightmare.
Given the other features of TS lend very well to enterprise-scale applications, it seems odd this isn't already possible. Is it possible to find out if this is on the roadmap?
Fantastic work as ever guys. Having union types is going to greatly improve the typings we can create.
On that note I started adding union types to the AngularJS typings today and found myself a little stumped when trying to work out the syntax for having a union type that includes a function. I figured it out in the end but it wasn't obvious and I couldn't find any docs with an appropriate example when I looked.
For anyone else who is pondering here's a slightly convoluted example of the syntax. Hopefully it explains it well enough:
“`
interface WithUnionTypes {
iCouldBeAStringOrAFunctionThatTakesANumberParameterAndReturnsAString: string|{(aParameter: number) : string;}
}
“`
Thanks again!
Every TS project I do, I keep wanting abstract classes, and 'protected' properties.
They're compiling fine, but inside VS2013 it shows my string templates as syntax errors. "Identifier is expected. ; is expected."
Great news! Will be async/await in next versione 1.5?
Wow! this is exciting, thanks! 😉
Keep up the good work.
"TypeScript now supports using ‘let’ and ‘const’ in addition to ‘var’. These currently require the ES6 output mode, but we’re are investigating relaxing this restriction in future versions."
Hooray!
Very excited to use the new features, thanks for such fast frequent releases!
Couple of breaking changes showed up in my code, using noImplicitAny:
interface MyType {
getNames(): string[];
}
function foo(): MyType {
return {
getNames() { return []; }
};
}
The "return []" in getNames now errors because the array is of implicit type any[]. In 1.3 this was allowed, and made sense I think. OTOH, no great hardship to require the explicit type.
Also:
interface Point { x?: number; y?: number; }
var p = getPosition() || {};
In 1.3. the type of p was Point, in 1.4 it's {}. Note that {} was compatible because x and y are optional. Again, can be worked around with:
var p: Point = getPosition() || {};
I don't think either of these are bugs.
While tighter scoping and localization are great, allowing two variables with the same name in concentric scopes seems like promoting confusion.
You might look at C# where:
static void Main(string[] args)
{
var x = 1;
{
var x = 2;
}
}
results in:
A local variable named 'x' cannot be declared in this scope because it would give a different meaning to 'x', which is already used in a 'parent or current' scope to denote something else
@Marcus – yes, multi-file external modules are still on the backlog. We're trying to clear through ES6 stuff first.
@John Reilly – you may just want to use function overloads for that case, assuming I'm reading your question correctly.
@Jeff – 'protected' is already supported. 'abstract' is something we have prototypes for, but currently on backburner while we work on ES6.
@Gambero – Not sure when it will land… we're going through the design process for it now github.com/…/1664 and will work with a prototype for a while to make sure we're comfortable with it.
@Daniel Earwicker – there have been a couple minor updates to no implicit any, but I don't think they should have affected your example. Can you give us feedback on the issue tracker? github.com/…/issues
@Marcus re compilation of multiple TS files into a single commonJS module, with matching combined single d.ts file
Hopefully I'm not repeating workarounds that are causing you pain.
To create a single AMD module:
I'm using requireJs and using a shim config section to load my typescript generated moduleName var. I assume commonJS has something similar. I had to use shim init function (not usual exports) to get the moduleName var since the requireJs optomiser wrapped it (Typescript does not use var window.moduleName, but var moduleName).
To create a single declaration file:
Use build command line "type". See my code at stackoverflow.com/…/27516591
@Mohamed Hegazy – My team was not able to reproduce the save & compile bug with this version thus far. Thank you!
Is support coming for VS 2012?
@Florian – current development is on VS 2013 and VS 2015. You can get TypeScript 1.0.1 for VS 2012: blogs.msdn.com/…/announcing-typescript-1-0-1.aspx
Regarding my issue with templates in the IDE: it was a ReSharper issue 🙂
@Jonathan Turner
Sorry – my comment wasn't terribly clear. I was just concerned that there weren't good examples out there of how functions could be included in union types. I stumbled upon the syntax after experimenting a little and then wrote it up to try and make it better known:
blog.icanmakethiswork.io/…/typescript-using-functions-with-union-types.html
It would be good if this usage of union types were expressed more publicly than my blog though. I tried to update the wiki here:
github.com/…/What%27s-new-in-TypeScript%3F
But unfortunately it appears not to be clonable…. Results in this error from Git: error: Invalid path 'What's-new-in-TypeScript?.md'
Will TypeScript be used for creating Universal Apps in Windows 10?
Hi, great update!
When it will be available in Azure Websites environment?
Why "Union Type"? I think this can be replaced by generic type instead:)
Are there any differences?
I love template strings! I've been waiting for a feature like this. However, I can't seem to find its behavior listed anywhere in the 1.4 language specification. What's going on? http://www.typescriptlang.org/…/TypeScript%20Language%20Specification.pdf
TS Team, let me just say that I think you're all doing an outstanding job (you knew there was a but coming right) but, I'm really surprised and a little disappointed to find that these excellent type alias' cannot be used to instantiate a class?! Any particular reason for this?
@Tony Type aliases are for aliasing types specifically. In TypeScript, you instantiate not with a type but instead with a constructor function, just like in JavaScript. You can think of TypeScript merely as a thin layer on top of JavaScript that helps tool the JavaScript patterns.
In short, you instantiate from functions rather than types, so aliases have no effect here. You can alias the classes as well, if you like by using 'var myAlias = My.Long.Class.Name' to get the constructor, or similarly using the 'import' keyword rather than 'var'.
The template string function is working but in the editor, intellisence is not happy with the format.
Also the editor tries very hard to reformat the { } combination into multiple lines. Is there anyway to fix these two problems?
Thanks.
While it's awesome to see more support for ES6, VS2013 makes it near impossible to utilize any of these features. Switching to VS2015 on a primary system isn't a feasible option while it's in CTP.
I'm wondering if there is any reason 'var' should not act everywhere like 'let'?
@Tony – The way 'var' works matches how 'var' works in JavaScript. TypeScript can really be thought of as functioning exactly like JavaScript with a few additional features, rather than changing anything fundamental about JavaScript itself.
The link to Visual Studio 2013 in the second paragraph is broken
@GaryB
Is the link still broken for you? It seems to be working for me, so I'm not sure if it was an intermittent problem.
It seems like other ES6 to ES 5 transpilers handle 'let'. Can it not be done in Typescript?
For now, in order to use 'let', do we have to first target Typescript to ES6 output mode and then transpile it down to ES5 using some other tool?
I'm very excited about union types. That was my #1 most desired feature for Typescript. The second was type aliasing. So you nailed it, thank you.
I'm having a hard time installing it, but I posted that question on SO.
Multi-line strings with template substitutions within backticks “ Hurray! That is really nice. Newlines are converted to n, backticks to normal double quoted parentheses and substitutions are concatenated with +, nicely spaced. Works well in ES5-mode.
See http://www.sympad.nl/connect4/ for my all-TypeScript project Connect-4
enum inside class not supported yet 🙁
I create a nice template to develop with typescript or just develop web project!
It include a Gulp with Scss & js minify, livereload, watch files, font & image minify. I also include pug if you want to use it. You simply can use html if you want.
I also include Foundation(framework).
Why typescript is nice?
You can use it to “import” .js in your main.js like an main.scss that you @import all your style.scss.
https://github.com/t1gu1/front-end-typescript-template
Follow me and leave comments on my github! ^^