This overview page contains a shortened version of all the release notes for TypeScript. Because this page is so big. code samples have their interactive elements disabled. Show
TypeScript 4.2Smarter Type Alias PreservationTypeScript has a way to declare new names for types called type aliases. If you’re writing a set of functions that all work on
In TypeScript 4.2, our internals are a little smarter. We keep track of how types were constructed by keeping around parts of how they were originally written and constructed over time. We also keep track of, and differentiate, type aliases to instances of other aliases! Being able to print back the types based on how you used them in your code means that as a TypeScript user, you can avoid some unfortunately humongous types getting displayed, and that often translates to getting better For more information, check out the first pull request that improves various cases around preserving union type aliases, along with a second pull request that preserves indirect aliases. Leading/Middle Rest Elements in Tuple TypesIn TypeScript, tuple types are meant to model arrays with specific lengths and element types.
In TypeScript 4.2, rest elements specifically been expanded in how they can be
used. In prior versions, TypeScript only allowed However, now rest elements can occur anywhere within a tuple - with only a few restrictions.
Even though JavaScript doesn’t have any syntax to model leading rest parameters, we were still able to declare For more details, see the original pull request. Stricter Checks For The in OperatorIn JavaScript,
it is a runtime error to use a non-object type on the right side of the
This check is fairly conservative for the most part, so if you have received an error about this, it is likely an issue in the code. A big thanks to our external contributor Jonas Hübotter for their pull request! --noPropertyAccessFromIndexSignatureBack when TypeScript first introduced index signatures, you could only get properties declared by them with “bracketed” element access syntax like
This ended up being cumbersome in situations
where we need to work with objects that have arbitrary properties. For example, imagine an API where it’s common to misspell a property name by adding an extra
To make these types of situations easier, a while back, TypeScript made it possible to use “dotted” property access syntax like In some cases, users would prefer to explicitly opt into the index signature - they would prefer to get an error message when a dotted property access doesn’t correspond to a specific property declaration. That’s why TypeScript introduces a new flag called You can understand this feature in more detail by reading up on the corresponding pull request. We’d also like to extend a big thanks to Wenlu Wang who sent us this pull request! abstract Construct SignaturesTypeScript allows us to mark a class as abstract. This tells TypeScript that the class is only meant to be extended from, and that certain members need to be filled in by any subclass to actually create an instance. TypeScript 4.2 allows you to specify an
Adding the This feature allows us to write mixin factories in a way that
supports abstract classes. For example, in the following code snippet, we’re able to use the mixin function
Note that You can read up more on abstract construct signatures on its pull request. Understanding Your Project Structure With --explainFilesA surprisingly common scenario for TypeScript users is to ask “why is TypeScript including this file?“. Inferring the files of your program turns out to be a complicated process, and so there are lots of reasons why a specific combination of That’s why TypeScript now provides an
When using this option, the TypeScript compiler will give some very verbose output about why a file ended up in your program. To read it more easily, you can forward the output to a file, or pipe it to a program that can easily view it.
Typically, the output will start out by listing out reasons for including
Right now, we make no guarantees about the output format - it might change over time. On that note, we’re interested in improving this format if you have any suggestions! For more information, check out the original pull request! Improved Uncalled Function Checks in Logical ExpressionsThanks to further improvements from Alex Tarasyuk, TypeScript’s uncalled function checks now apply within Under
For more details, check out the pull request here. Destructured Variables Can Be Explicitly Marked as UnusedThanks to another pull request from Alex Tarasyuk, you can now mark destructured variables as unused by prefixing them with an
underscore (the
Previously, if For more details, take a look at the full change. Relaxed Rules Between Optional Properties and String Index SignaturesString index signatures are a way of typing dictionary-like objects, where you want to allow access with arbitrary keys:
Of course, for any movie title not yet in the dictionary,
TypeScript 4.2 allows this assignment. However, it does not allow the assignment of non-optional properties with
The new rule also does not apply to number index signatures, since they are assumed to be array-like and dense:
You can get a better sense of this change by reading up on the original PR. Declare Missing Helper FunctionThanks to a community pull request from Alexander Tarasyuk, we now have a quick fix for declaring new functions and methods based on the call-site! Breaking ChangesWe always strive to minimize breaking changes in a release. TypeScript 4.2 contains some breaking changes, but we believe they should be manageable in an upgrade.
|