Announcing TypeScript 1.7
Today, we are thrilled to announce the release of TypeScript 1.7 along with the availability ofVisual Studio 2015 Update 1. This releaseenables async/await by default for ECMAScript 6 (ES6) targets. It also adds support for polymorphic ‘this’ typing, proposed ECMAScript 2016 exponentiation syntax, and ES6 module targeting. For a complete change list, check out ourroadmap on GitHub.
As always, you can get your hands on TypeScript 1.7 forVisual Studio 2015 Update 1,Visual Studio 2013,on npm, or straight from thesource.
Async/Await for ES6 targets
With the 1.7 release, TypeScript now supportsAsync functions for targets that haveES6 generator support enabled (e.g. node.js v4 and above). Functions can now be prefixed with theasync keyword designating it as an asynchronous function. Theawait keyword can then be used to stop execution until anasync function’s promise is fulfilled. Following is a simple example:
“use strict”;
// printDelayed is a ‘Promise<void>’
async function printDelayed(elements: string[]) {
for (const elementof elements) {
await delay(200);
console.log(element);
}
}
async function delay(milliseconds: number) {
returnnew Promise<void>(resolve => {
setTimeout(resolve, milliseconds);
});
}
printDelayed([“Hello”,“beautiful”,“asynchronous”,“world”]).then(() => {
console.log();
console.log(“Printed every element!”);
});
We are working on bringing async/await support in TypeScript for other targets, including a breadth of browsers, which might not have ES6 generators support. For more information on current implementation of async/await and how to use it, see ourprevious blog post.
Polymorphic this Typing
After muchcommunity discussion and feedback, TypeScript 1.7 adds a new polymorphic this type. A this type can be used in classes and interfaces to representsome type that is a subtype of the containing type (rather than the containing type itself). This feature makes patterns such as hierarchical fluent APIs much easier to express.
interface Model {
setupBase():this;
}
interface AdvancedModel extends Model {
setupAdvanced():this;
}
declare function createModel(): AdvancedModel;
newModel = newModel.setupBase().setupAdvanced();// fluent style works
For a deep dive on this typing, checkout theTypeScript Wiki.
As a part of supporting the feature, TypeScript 1.7 has made changes in inferring the type from this. In a class, the type of the value this will be inferred to the this type, and subsequent assignments from values of the original type can fail. As a workaround, you could add a type annotation for this. A code sample with recommended work around, along with a list of otherpotentially breaking changes is available at GitHub.
ES6 Module Emitting
TypeScript 1.7 adds es6 to the list of options available for the –module flag and allows you to specify the module output when targeting ES6. This provides more flexibility to target exactly the features you want in specific runtimes. For example, it is now a breeze to target Node.js v4 and beyond, which doesn’t support ES6 modules (but does support several other ES6 features).
//tsconfig.json targeting node.js v4 and beyond
{
“compilerOptions”: {
“module”:“commonjs”,
“target”:“es6”
}
}
ES7 Exponentiation
Finally, a little syntactic sugar! The ECMAScript committee recently moved theExponentiation Operator proposal tostage 3. So we decided it was ready for TypeScript to adopt, and added support for it in TypeScript 1.7.
let cubed = 2 ** 3; // same as: 2 * 2 * 2
num **= 2;// same as: num = num * num;
Say goodbye toMath.pow()!
What’s Next?
We are excited to announce all the new improvements we’ve made in this release, and as always, we would love to hear your feedback. Everything we do is easily viewable on ourGithub. If you’re interested in weighing in on the future of TypeScript, we encourage you to check out our existingissues, throw us apull request, or just come hang out with the team ongitter.
Author
0 comments
Discussion is closed.

