Better Programming

Advice for programmers.

Follow publication

Member-only story

What’s New in TypeScript 4.2?

Jose Granja
Better Programming
Published in
5 min readFeb 23, 2021

Laptop screen showing code
Photo by Vishnu R Nair on Unsplash

Typescript’s 4.2 was just released on Tuesday 23 of February 🎉. What awesome features does this release bring? What impact does it have in your daily life as a Developer? Should you immediately update?

Here, I will be going through all the most exciting new features. Here is a summary:

  • Smarter type alias preservation
  • Leading/middle rest elements in tuple types
  • Stricter checks for the in operator
  • --noPropertyAccessFromIndexSignature
  • Template literal expressions have template literal types
  • Improved uncalled function checks in logical expressions
  • Better understanding of the compile process
  • Support for Abstract Constructor Types

To get an editor with the latest Typescript version use Visual Studio Code Insiders. You can use a plugin alternatively for VS Code.

If you just want to have a play while reading the article you can use the Typescript Playground here. It is a fun and super easy tool to use.

Smarter Type Alias Preservation

Sometimes TypeScript just doesn’t resolve types properly. It may return the correct types but just not return the correct alias. The alias could be important and shouldn’t be lost along the way.

Let’s check this function:

Notice that an undefined type needs to be added to the method return type as it’s returning undefined on some scenarios.

Before 4.2 the return of type divisablePer0 is number | bigint | undefined. That type is indeed correct but we have lost some information. The alias BasicPrimitive got lost in the process, which is a handy piece of information to have.

If we do the same on TypeScript 4.2 we get the correct alias:

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Jose Granja
Jose Granja

Written by Jose Granja

Top Writer @Medium | 1M+ Views | Lead FE @Lingoda | I write weekly about web-development topics 📰 Support me at https://dioxmio.medium.com/membership 🙇

No responses yet

Write a response