Member-only story
What’s New in TypeScript 4.2?
Another awesome incremental upgrade for TypeScript
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: