What’s New in TypeScript 4.8?. Build Improvements, better inference… | by Jose Granja | Aug, 2022

Build Improvements, better inference for Template String types, and more

Image by author
Image by author

The TypeScript 4.8 version is planned to be released on the 23th of August 🎉. This time we won’t see an exciting release. Especially if we compare it to the 4.7.. It is more focused on improving some existing features and bumping up its build times.

In this article, I will highlight the most relevant new features. Here is the summary:

  • Improve intersection reduction and CFA for truthy, equality, and typeof checks.
  • Improved Inference for infer Types in Template String Types
  • Build improvements
  • Support error when comparing with object/array literals.

As always, you can follow up on the example by using the TypeScript web playground here.

There are changes introduced when using --strictNulChecks that will bring a bit more consistency. Many of these changes revolve around the unconstrained type variables.

The empty object {} is now a supertype of all types except null and undefined. That means that unknown can be expressed as {} | null | undefined. For an arbitrary, T its intersection with {} will be considered as T.

Also, it means, that now we get a more correct typing with:

The above error signals that T could be null or undefined and hence the error.

Let’s see some other practical examples:

Note the T4, T6 have not changed between TS 4.7 and TS 4.8 as {} was already a superset of those types. T8 and T9 are unchanged since they were never intersection with the {} type.

The NonNullable mapped type has been simplified.

As now {} is a superset of non-nullable types its interception would mean that it is not nullable if different from never. As we have seen above the intersection of any non-nullable with {} would return the same type.

What cool things can you do now? You can narrow down the unknown type.

Let’s see what the current state was prior to 4.7. You would never be able to narrow the unknown type further to a non-nullable value further. It will always cycle back to unknown. Why? Because {} was not a superset of all non-nullable:

From 4.8 we can:

Find out more by checking the PR here.

The Template String Types being one of the newest features it tends to get an update in each TypeScript release.

As a refresher, we can use Template String Types with a placeholder value.

As a refresher inference is used to provide type information when there is no explicit type annotation. Let’s first see a basic example:

The above is great, however, we can do a more powerful inference. The infer keyword lets us run inference within a condition in a conditional type.

We can rewrite the above example to run the infer conditionally on a given pre-condition.

The above code is useful in Template String Type as they might be composed of different types. The Is<Infer..> discriminates conditionally from those. The result of this complex logic is getting addressed in this release. Let’s see the difference:

In the example above, 100 is more accurate as a type than number.

Find out more by checking the PR here.

It’s known that one of TypeScript’s Achilles heels has always been its performance and build time. A bunch of optimizations has been done around --build, --watch, and -incremental. In a nutshell, it all revolves around not re-calculating the timestamps as they are cached.

How big are those changes? The lowest improvement scenario would mean a 10% decrease whilst the best would mean a 40%. Those numbers can be seen here and are the result of checking 1994 large projects.

Find out more by checking the PR here.

This is a nice enhancement that might catch errors early. In TypeScript, you might compare by using the == or === operator. When using objects it will check if they both point to the same reference. It won’t compare their value.

As part of this release, TypeScript will error out those scenarios for us:

If you know the language you are not likely to make this mistake. However, this will help to learn the intricacies of JavaScript.

Find out more by checking the PR here.

That is it. We have a solid release that helps lots of little here and there. It is awesome when we see some core issues worked out. That means that the language is getting more and more solid prior to adding more features.

If you need any more details you can follow the TypeScript 4.8 release plan here.

The improvements on the build times should be enough to get everyone pretty excited and install right away this new version.


News Credit

%d bloggers like this: