typescript background image microsoft

TypeScript, Microsoft’s free and open source JavaScript subset, has been given significant upgrades. Available since 2012, this marks the first major update since its release.

The beta follows an announcement in April which confirmed that non-nullable types would be included. The new version has two types, one that has a single undefined value, and another that is called null type. A number and a string is undefined, a Union Type that can join single and null types. “If you want to have a thing that can be number or undefined, you write it as a union type,” Hejlsberg said.

Features

The update also contains a host of other features, including a new workflow for getting .d.ts files. Microsoft goes into detail about the following improvements in their blog post:

Control Flow Analysis for Types

TypeScript’s support for handling nullable types is possible thanks to changes in how types are tracked throughout the program. In 2.0, we’ve started using control flow analysis to better understand what a type has to be at a given location.

Easier Module Declarations

Sometimes you want to just tell TypeScript that a module exists, and you might not care what its shape is. We made it easier and got rid of the boilerplate. Now you can import any path that starts with foo/ and TypeScript will assume it exists. You can take advantage of this if your module loader understands how to import based on a certain pattern too. Now whenever you import a path ending with !text, TypeScript will understand that the import should be typed as a string.

Non-nullable Types

null and undefined are two of the most common sources of bugs in JavaScript. Before TypeScript 2.0,null and undefined were in the domain of every type. That meant that if you had a function that took astring, you couldn’t be sure from the type alone of whether you actually had a string – you might actually have nullIn TypeScript 2.0, the new --strictNullChecks flag changes that. string just means string andnumber means number.

To start implementing these new features you need to download the latest TypeScript 2.0 Beta for Visual Studio 2015. Those that are new to the subset can view the official documentation, which walks you through the building of a simple web application.