Paul Krill
Editor at Large

TypeScript 2.0 borrows from Rust and Swift

news
Aug 31, 20162 mins

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

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.

Paul Krill

Paul Krill is editor at large at InfoWorld. Paul has been covering computer technology as a news and feature reporter for more than 35 years, including 30 years at InfoWorld. He has specialized in coverage of software development tools and technologies since the 1990s, and he continues to lead InfoWorld’s news coverage of software development platforms including Java and .NET and programming languages including JavaScript, TypeScript, PHP, Python, Ruby, Rust, and Go. Long trusted as a reporter who prioritizes accuracy, integrity, and the best interests of readers, Paul is sought out by technology companies and industry organizations who want to reach InfoWorld’s audience of software developers and other information technology professionals. Paul has won a “Best Technology News Coverage” award from IDG.

More from this author