TypeScript 2.0 borrows from Rust and Swift

The latest version adds a tagged unions type capability and more literal types, enhancing type safety and reducing tedium

TypeScript 2.0 borrows from Rust and Swift
Credit: Shutterstock

With the TypeScript 2.0 release candidate, Microsoft's typed superset of JavaScript mimics a tagged unions type capability featured in languages like Rust and Swift. Through this feature, developers can cut down on writing boilerplate code.

Daniel Rosenwasser, Microsoft program manager for TypeScript, used an example in which two types, "Circle" and "Square," have a union type, "Shape." In TypeScript 1.8, writing a function to get the area of Shape required a type assertions for each type, whereas TypeScript 2.0 understands how to discriminate based on the "kind" field.

"Tagged unions are an exciting new feature that brings functionality from languages like F#, Swift, Rust, and others to JavaScript while embracing the way that people write JavaScript today," Rosenwasser said. Tagged unions make it easier to get type safety using JavaScript patterns developer would write today. "For example, libraries like Redux will often use this pattern when processing actions," he noted.

The release candidate also features more literal types. "We wanted to give some more love to types other than just 'string'," Rosenwasser said. "In 2.0, each unique 'boolean,' 'number,' and 'enum' member will have its own type." The new version adds globs, for writing out wild cards for paths when working with tsconfig.json file capability, reducing tedium for developers.

The release candidate is downloadable for use with Visual Studio 2015 and can be accessed via NuGet or NPM, using NPM install -g typescript@rc.

From CIO: 8 Free Online Courses to Grow Your Tech Skills
View Comments
Join the discussion
Be the first to comment on this article. Our Commenting Policies