BT

InfoQ Homepage News Microsoft Releases TypeScript 2.0 RC

Microsoft Releases TypeScript 2.0 RC

Bookmarks

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

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • Good news. Waiting for Dart 2.0

    by Andrew Mezoni /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    I wonder when it will be released (Dart 2.0)?
    It was promised at the beginning of 2016, but for some reason everything now quiet.
    Probably, some problems with the developers?

    But programmers from Microsoft just great, not like the others from...

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.