Microsoft TypeScript 3.0: What’s New?

Language provides a richer environment for spotting bugs as you type code.

Tech giant Microsoft recently released a new version of its programming language TypeScript (a superset of JavaScript with static typing), with new features such as function parameter listing as tuple types and a new datatype called ‘unknown’.

TypeScript helps enable the construction of large-scale JavaScript applications. The TypeScript compiler emits clean readable JavaScript that runs in any ECMAScript runtime like most browsers and Node.js.

As the Microsoft team said of 3:0’s release: “At its core, this experience means analysing your code to catch things like bugs and typos before your users run into them.”

Few programmers would begrudge that.

These are the most notable features.

A New Unknown Datatype

A data type, called “unknown”? Yes, the TypeScript team have created this datatype to improve type safety, which the team claims will help programmers to safely declare types of data that are not known until a later point (runtime).

Traditionally, developers have used the keyword ‘any’ which supports any data type, to handle such scenarios, however this approach does not enforce type safety because ‘any’ can be assigned to anything.

In contrast, ‘unknown’ can only be assigned to a variable once a data type has been subsequently defined using type assertion (essentially telling the data what to look like).

The team states in the release notes: “TypeScript 3.0 introduces a new top type unknown. unknown is the type-safe counterpart of any. Anything is assignable to unknown, but unknown isn’t assignable to anything but itself and any without a type assertion or a control flow based narrowing. Likewise, no operations are permitted on an unknown without first asserting or narrowing to a more specific type.”

Function Parameter Lists as Tuple Types

Parameter lists as tuple types enable developers to interact with function parameters as tuples, meaning that parameters can be assigned types using tuples.

The TypeScript team explains that this will give developers more power when handling data passed into function parameters: “With these features it becomes possible to strongly type a number of higher-order functions that transform functions and their parameter lists”.

This feature was asked for via GitHub in 2015, by a user who does not appear to be on the team themselves; good to see Microsoft listening to the community. With even more features outlined in the release notes, it’s clear that Microsoft’s TypeScript continues to deliver real world value to developers.