TypeScript 2.0 borrows from Rust and Swift

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.

IDG Insider


« Fujitsu now making DRAM killer with 1,000x performance boost


Windows 10 Mobile review: Small fixes show this plucky phone OS won't give up »
IDG News Service

The IDG News Service is the world's leading daily source of global IT news, commentary and editorial resources. The News Service distributes content to IDG's more than 300 IT publications in more than 60 countries.

  • Mail


Do you think your smartphone is making you a workaholic?