TypeScript 3.1 releases with typesVersions redirects, mapped tuple types

2 min read

After announcing TypeScript 3.1 RC version last week, Microsoft released TypeScript 3.1 as a stable version, yesterday. This release comes with support for mapped array and tuple types, easier properties on function declarations, typesVersions for version redirects, and more.

Support for mapped array and tuple types

TypeScript has a concept called ‘mapped object type’ which can generate new types out of existing ones. Instead of introducing a new concept for mapping over a tuple, mapped object types now just “do the right thing” when iterating over tuples and arrays.

This means that if you are using the existing mapped types like Partial or Required from lib.d.ts, they will now also automatically work on tuples and arrays. This change will eliminate the need to write a ton of overrides.

Properties on function declarations

For any function or const declaration that’s initialized with a function, the type-checker will analyze the containing scope to track any added properties. This enables users to write canonical JavaScript code without resorting to namespace hacks.

Additionally, this approach for property declarations allows users to express common patterns like defaultProps and propTypes on React stateless function components (SFCs).

Introducing typesVersions for version redirects

Users are always excited to use new type system features in their programs or definition files. However, for the library maintainers, this creates a difficult situation where they are forced to choose between supporting new TypeScript features and not breaking its older versions. To solve this, TypeScript 3.1 introduces a new feature called typesVersions.

When TypeScript opens a package.json file to figure out which files it needs to read, it will first look for the typesVersions field. The field will tell TypeScript to check which version of TypeScript is running. If the version in use is 3.1 or later, it figures out the path you’ve imported relative to the package and reads from the package’s ts3.1 folder.

Refactor from .then() to await

With this new refactoring, you can now easily convert functions that return promises constructed with chains of .then() and .catch() calls to async functions that uses await.

Breaking changes

  • Vendor-specific declarations removed: TypeScript’s built-in .d.ts library and other built-in declaration file libraries are partially generated using Web IDL files provided from the WHATWG DOM specification. While this makes keeping lib.d.ts easier, many vendor-specific types have been removed.
  • Differences in narrowing functions: Using the typeof foo === “function” type guard may provide different results when intersecting with relatively questionable union types composed of {}, Object, or unconstrained generics.

How to install this latest version?

You can get the latest version through NuGet or via npm by running:

npm install -g typescript

According to their roadmap, TypeScript 3.2 is scheduled to be released in November with strictly-typed call/bind/apply on function types.

To read the full list of updates, check their official announcement on MSDN.

Read Next

TypeScript 3.1 RC released

TypeScript 3.0 is finally released with ‘improved errors’, editor productivity and more

How to work with classes in Typescript