Paul Krill
Editor at Large

TypeScript 4.9 intros operator for finding coding mistakes

news
Nov 16, 20223 mins

New ‘satisfies’ operator validates that the type of an expression matches some type, catching many possible errors.

Microsoft’s TypeScript 4.9, a planned upgrade to the popular, strongly typed language that builds on JavaScript, is now available as a production release. The update features a satisfies operator that can catch errors.

With satisfies, developers can validate that the type of an expression matches some type, without changing the resulting type of the expression. This operator can be used to catch possible errors, such as ensuring that an object has all the keys of some type, but no more than that.

Also in TypeScript 4.9, the in operator has been made more powerful when narrowing types that do not list the property. Instead of leaving them as is, the language will intersect their types with Record<”property-key-being-checked”, unknown>.

TypeScript 4.9 also tightens up checks around how in is used, to ensure that valid property keys are being used.

Introduced November 15, following beta and release candidate stages, TypeScript 4.9 can be downloaded through NuGet or via NPM using the following command:

npm install -D typescript

Other new capabilities and improvements in TypeScript 4.9:

  • File watching is powered by file system events by default, only falling back to polling if developers fail to set up event-based watchers. This should provide a less resource-intensive experience when running –-watch mode or running with a TypeScript-powered editor like Visual Studio Code or Visual Studio.
  • Promise.resolve now uses the Awaited type to unwrap Promise-like types passed to it. This means that it more often returns the right Promise type, but that improved type can break existing code if it was expecting any or unknown instead of a Promise.
  • TypeScript now errors on direct comparisons against the NaN value and will suggest some variation of Number.isNAN instead.
  • TypeScript now supports an upcoming feature in ECMAScript, called auto-accessors, which are declared just like properties in classes, except that they are declared with the accessor keyword.
  • To improve performance, the forEachChild function has been rewritten to use a function table lookup instead of a switch statement across syntax nodes. Also, the way TypeScript preserves information about a type in the true branch of a conditional type has been optimized.
  • To optimize substitution types, SubstitutionType objects no longer contain the substitute property representing the effective substitution. Instead, they just contain the constraint property.

TypeScript 5.0 is due as a beta release on January 24, 2023, with a release candidate set for February 28 and a production release slated to arrive March 14. TypeScript 4.8 shipped on August 25, bringing correctness and consistency improvements as well as file watching fixes.

TypeScript has been on an upswing. CircleCI’s 2022 State of Software Delivery report found that TypeScript had surpassed JavaScript as the most popular devops language. CircleCI cited developer-friendliness as a reason for the surge.

Predecessor TypeScript 4.8 shipped on August 25, bringing correctness and consistency improvements as well as file watching fixes.

Paul Krill

Paul Krill is editor at large at InfoWorld. Paul has been covering computer technology as a news and feature reporter for more than 35 years, including 30 years at InfoWorld. He has specialized in coverage of software development tools and technologies since the 1990s, and he continues to lead InfoWorld’s news coverage of software development platforms including Java and .NET and programming languages including JavaScript, TypeScript, PHP, Python, Ruby, Rust, and Go. Long trusted as a reporter who prioritizes accuracy, integrity, and the best interests of readers, Paul is sought out by technology companies and industry organizations who want to reach InfoWorld’s audience of software developers and other information technology professionals. Paul has won a “Best Technology News Coverage” award from IDG.

More from this author