What's new in TypeScript 4.2?
Version 4.2 of TypeScript was released on 23 February, 2021, and with it a number of nice features, bug fixes, and performance improvements. Here are the most important ones you should be aware of.
TypeScript 4.2’s top enhancements
More flexible rest elements in tuple types
Previous to 4.2, rest elements could only appear at the end of the tuple type. If we were modeling the state of a two-player game, for example, we might use this tuple type to hold our two Player
s and a list of the Move
s they’ve played:
With 4.2, the Move
s don’t have to be at the end of the type; they could be in the middle or even at the beginning. If desired, we can now expand our type as follows, to include which player’s turn it is. For additional clarity, we’ll take advantage of TypeScript’s support for labeled tuple elements (which is not new in 4.2):
Increased visibility into which files are included in your compiled program
TypeScript’s compiler (tsc
) now includes a new flag, --explainFiles
, that outputs a list of files included in the compilation as well as basic reasoning behind why they’re there, in a simple text format. This can be very helpful when developing or fine-tuning compiler configuration in tsconfig.json
.
This feature is a nice first step toward debugging build time issues, and I expect to grow more robust and powerful in future releases (for example, a JSON output format for ingestion into other tools for more advanced analysis).
Given a default TypeScript config and an index.ts
file with a simple console.log('hello, world!');
, here is some example output from the --explainFiles
flag:
Try it yourself using this example repository (complete with devcontainer.json) on GitHub.
Better support for unused destructured variables
When destructuring tuples or arrays, there are times when the elements you are interested in don’t appear at the beginning of the list. In these cases, “throwaway” variable names such as _
or a
, b
, c
, etc., are used for the elements of no interest.
With the noUnusedLocals
compiler option on, however, these unused local variables would cause TypeScript to throw an error until version 4.2.
Now, simply prepend the unused variable names with _
and TypeScript will happily ignore these variables and will not throw an error if they are unused. As an example, this new feature would be particularly useful when extracting bits of data from the rows of a CSV spreadsheet:
Prefixing unused variable names with _
is a common convention in situations like these; this is an example of tool authors building thoughtfully around and supporting the existing behavior of their users.
Smarter type system and performance improvements
As with any TypeScript release, there were also a number of smaller enhancements that are not groundbreaking on their own but they make TypeScript incrementally better and more comfortable to use. To name just a few:
- A helpful error when trying to use the
in
operator on a primitive type. This is normally a runtime error (in JavaScript) but is now caught at compile time in TypeScript - An internal limit on tuple size in conjunction with the spread syntax, to improve compilation performance
- Better parsing and interpretation of vanilla JavaScript files
- A new flag called
--noPropertyAccessFromIndexSignature
that can help reduce errors from object property name misspellings under certain situations
More details and resources
A full list of all the enhancements can be seen on the TypeScript project’s releases page on GitHub, as well as the release announcement on the TypeScript blog. Those are the top highlights of the TypeScript 4.2 release. To dive deeper in to the changes or learn more about TypeScript generally, check out the following resources: