Microsoft releases TypeScript 4.6 with a score of new features

JavaScript code displayed on monitor
(Image credit: Shutterstock)

Microsoft has released the latest version of TypeScript, version 4.6, which brings with it an array of features including some that weren’t included in the beta or release candidate (RC) versions.

Among these is the ability for developers to narrow types based on discriminate properties, something that would throw an error in previous versions of TypeScript.

Microsoft said it comes down to each developer’s preference and how they like to destructure objects into properties.

“When destructuring individual properties into a const declaration, or when destructuring a parameter into variables that are never assigned to, TypeScript will check for if the destructured type is a discriminated union,” said Daniel Rosenwasser, senior program manager of TypeScript at Microsoft. “If it is, TypeScript can now narrow the types of variables depending on checks of other variables.”

Another of the never-before-seen features in TypeScript 4.6 is the support for es2022 in TypeScript’s --target option. It means features like class fields now have a stable output target where they can be preserved.

It also means more recently added TypeScript functionality like the at() method on arrays, Object.hasOwn, or the ‘cause’ option on a new Error can be used with the new --target setting or with --lib es2022.

A feature that was present in the previous RC release but went undocumented was the suggestions for mismatched JSDoc parameter names. Developers can document parameters using an @param tag but when these fall out of date TypeScript editors will now provide suggestions for when parameter names don’t match between a function and its JSDoc comment.

One change from the beta that made it to the current release is the removal of unnecessary arguments when programming in --jsx react-jsx mode. When compiling certain code, JavaScript would produce code containing a ‘void 0’ argument that simply wasn’t necessary. Overall bundle sizes can be reduced as a result of the argument’s removal, said Rosenwasser.

Further code improvements have been made to classes; developers will now be able to write their constructor’s code before declaring super().

Classes made JavaScript, a language on which TypeScript builds, require the developer to call super() before referring to ‘this’. The rule used to apply to TypeScript too but Rosenwasser said the langauge was too strict in how it enforced the rule - it was a “cheap” implementation that rejected a lot of valid code.

There are a host of other changes brought to TypeScript including, but not limited to, an expansion of binding errors in JavaScript files. The full release notes can be found on Microsoft’s TypeScript developer blog.

Connor Jones
News and Analysis Editor

Connor Jones has been at the forefront of global cyber security news coverage for the past few years, breaking developments on major stories such as LockBit’s ransomware attack on Royal Mail International, and many others. He has also made sporadic appearances on the ITPro Podcast discussing topics from home desk setups all the way to hacking systems using prosthetic limbs. He has a master’s degree in Magazine Journalism from the University of Sheffield, and has previously written for the likes of Red Bull Esports and UNILAD tech during his career that started in 2015.