The latest major version of TypeScript, the popular superset of JavaScript that adds optional static typing to the language, is now here. On the 20th of August, Microsoft released TypeScript 4.0, and we thought of doing a quick recap of the new features.
Variadic Tuple Types
Spreads in tuple type syntax now can be generic. This means that higher-order operations on tuples and arrays can be represented even if the actual types being operated over are not known. When generic spreads are instantiated (or replaced with a real type) in these tuple types, they can produce other sets of array and tuple types. In addition, rest elements can occur anywhere in a tuple, not just at the end.
Labeled Tuple Elements
Tuple types can now provide labels. This will help your editor provide better hints when working with tuples.
Class Property Inference From Constructors
If we configure TypeScript in noImplicitAny mode, TypeScript 4.0 can use control flow analysis to determine the types of properties in classes.
Short-Circuiting Assignment Operators
JavaScript and many other languages support compound assignment operators. Compound assignment operators apply an operator to two arguments and then assign the result to the left side.
So many operators in JavaScript have a corresponding assignment operator, but there are three notable exceptions: logical and (&&), logical or (||), and nullish coalescing (??). To fill these gaps, TypeScript 4.0 introduces three new assignment operators: &&=, ||=, and ??=.
“unknown” on “catch” Clause Bindings
From the beginning of TypeScript, catch clause variables have always been typed as any. With TypeScript 4.0, the type of catch clause variables can be specified as unknown, with unknown being safer than any because it reminds developers that they must perform type-checking before operating on their values.
Custom JSX Factories
When using JSX, a fragment is a type of JSX element that allows us to return multiple child elements. In TypeScript 4.0, users can customize the fragment factory through a new jsxFragmentFactory option.
Speed Improvements in “build” mode with “— noEmitOnError”
In previous versions, compiling a program after a previous compile with errors under — incremental would be extremely slow when using the — noEmitOnError flag. Indeed, none of the information from the last compilation would be cached in a .tsbuildinfo file based on the — noEmitOnError flag. This has now been changed and TypeScript 4.0 boosts speed in the build mode.
Editor Improvements
The new JavaScript/TypeScript functionality improves editing scenarios in Visual Studio Code, Visual Studio 2017/2019, and more.
You can check out a partial list of editors that have support for TypeScript to learn more about whether your favorite editor has support to use new versions.
You can find all the new features of TypeScript 4.0 on the official website. And, if you want to test this latest version, you can access TypeScript 4.0 through NuGet or via npm with the following command: npm install -D typescript.
Discover here how the Web 3 will change internet.