BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Microsoft Releases TypeScript 2.0 RC

Microsoft Releases TypeScript 2.0 RC

Microsoft has released the TypeScript 2.0 release candidate, with tagged unions, and support for globs.

In the announcement on Microsoft's blog, Daniel Rosenwasser, program manager for TypeScript, said:

"This RC gives an idea of what the full version of 2.0 will look like, and we’re looking for broader feedback to stabilise and make 2.0 a solid release. Overall, the RC should be stable enough for general use, and we don’t expect any major new features to be added past this point.

On the other hand, lots of stuff has been added since 2.0 beta was released.

One of the most important updates in TypeScript's release candidate is its tagged unions.

"Tagged unions make it way easier to get type safety using JavaScript patterns you’d write today," Rosenwasser says. Where JavaScript may have lagged behind languages including F#, Swift and Rust, tagged unions allow TypeScript to discriminate based on the kind field, as seen below:

function getArea(shape: Shape) {
    switch (shape.kind) {
        case "circle":
            // Convert from 'Shape' to 'Circle'
            let c = shape as Circle;
            return Math.PI * c.radius ** 2;

        case "square":
            // Convert from 'Shape' to 'Square'
            let sq = shape as Square;
            return sq.sideLength ** 2;
    }
}

This is an improvement on TypeScript 1.8 that required type assertions for each type in shape.

The release also adds support for globs, expanding further on TypeScript 1.6's exclude field. TypeScript 2.0 allows developers to write out wildcards for paths, that Rossenwasser says can be as granular as needed, and are available to use in the new include field as well as the existing exclude, as below:

{
    "include": [
        "./src/**/*.ts"
    ],
    "exclude": [
        "./src/tests/**"
    ]
}

The TypeScript 2.0 release candidate requires Microsoft Visual Studio 2015 Update 3, which fixes several high-impact bugs, including "improved debugging reliability when you debug Visual C++ or CLI code" along with fixing "potential miscompilation of code-calling functions that resemble std::min/std::max on floating point values."

The reaction from the developer community to the RC release has been generally positive. On Reddit, Vheissu_ said "this is a massive leap forward for TypeScript. It feels impossible going back to just plain old ECMAScript now."

User cspotcode asked:

Will lib.dom.d.ts be updated to use discriminated unions for the Node.nodeType field? Or will that mess with the current types too much? It feels like, if Node.nodeType is still a number, then Element.nodeType can't be a numeric literal since that would violate Element extends Node. Node would have to be rewritten to be a union type instead of an interface.

Rossenwasser replied:

It's not out of the question - you can imagine that anything that returns a Node instead would return a new type that represents a union. It's not clear to me off the bat what the semantics of extending Node are, but if you have any ideas, we'd like to hear them! Feel free to open up an issue on us.

Asked when developers could expect the 2.0 final, Rossenwasser roughly estimated "a couple of weeks, but asked not to be held to it.

Rate this Article

Adoption
Style

BT