TypeScript 4.1 RC: New features for Microsoft’s JavaScript-based programming language

TypeScript 4.1 improves the programming language’s functionality and speed, while giving developers greater control.

typescript-vscode.jpg

TypeScript in Visual Studio Code.

Image: Microsoft

The 4.1 release candidate (RC) for Microsoft’s popular JavaScript off-shoot TypeScript is now available.


Must-read developer content




TypeScript is a language that builds on JavaScript by adding optional type checking, allowing it to catch common errors like miss-spelt properties and calling functions that have been written the wrong way.

SEE: Linux commands for user management (TechRepublic Premium)

In doing so, TypeScript can save developers time in catching errors, and provides fixes before they run code. It can be run anywhere JavaScript runs: in a web browser, in apps or on the Node.JS runtime environment.

Ultimately, TypeScript allows developers to build apps and programs based on JavaScript code more quickly.

TypeScript 4.1 RC introduces a handful of new features that improve the programming language’s functionality and speed, and give developers greater control when writing code.

This includes the new template literal type. This acts as a building block for compiling other string types and allows developers to model functions and APIs that expect a set of specific strings.

The template literal string type in TypeScript use the same syntax as template literal strings in JavaScript, but is used in type positions. When used with concrete literal types, it produces a new string literal type by chaining the contents together.

“This is pretty nice because string literal types can basically spell-check our string values,” explained TypeScript program manager Daniel Rosenwasser.

“We also like that string literals can be used as property names in mapped types. In this sense, they’re also usable as building blocks.”

TypeScript 4.1 also allows developers to re-map keys in mapped types by using ‘as’ as a clause.

Mapped types were introduced in TypeScript 2.1, allowing developers to create new types from existing ones by mapping over property types.

SEE: Top 5 programming languages for systems admins to learn (free PDF) (TechRepublic)

Until now, mapped types could only produce new object types with keys already provided to them. “Lots of the time you want to be able to create new keys, or filter out keys, based on the inputs,” explained Rosenwasser.

“That’s why TypeScript 4.1 allows you to re-map keys in mapped types with a new as clause. This makes it easier to create property names based on old ones, by using template literal types.”

The latest version of TypeScript also eases some restrictions on conditional types: in TypeScript 4.1, conditional types can now immediately reference themselves within their branches, making it easier to write recursive type aliases.

Rosenwasser warned that these should be used sparingly as they can increase type-checking time, and also hit recursion depth limits that can result in compile-time errors.

Additional new features in TypeScript 4.1 include better support for the JSDoc tag @see tag in editors for TypeScript and JavaScript, and support of React 17’s upcoming jsx and jsxs factory functions. This allows programmers to use functionality like go-to-definition in a dotted name following the tag. There’s also a new flag called –noUncheckedIndexedAccess for helping devs catch out-of-bounds errors.

The full list of new features in TypeScript 4.1 RC can be found here.