TypeScript 4.8 shines on intersection and union types

Update to Microsoft’s typed JavaScript introduces improvements to how intersection and union types work and how TypeScript narrows types.

Plastic, magnetic letters in compartmented boxes.
David Lofink (CC BY 2.0)

TypeScript 4.8, the latest upgrade to Microsoft’s strongly typed JavaScript variant, has reached production availability as of August 25. The new version brings correctness and consistency improvements, along with fixes to file watching on Linux and MacOS.

TypeScript 4.8 introduces improvements to consistency and correctness under strictNullChecks. These changes affect how intersection and union types function and are leveraged in how TypeScript narrows types. In one example, unknown is close in spirit to the union type {} | null | undefined because it accepts null, undefined, and any other type. TypeScript now recognizes this and allows assignments from unknown to {} | null | undefined. Changes in TypeScript 4.8 allow for sensible improvements in control flow and type narrowing, Microsoft said. Generic values also get narrowed.

As for the file-watching fixes, TypeScript has had a longstanding bug where the language has had a hard time with certain file changes in --watch mode and editor scenarios. Frequently, these occur on Unix systems. The situation was caused by assumptions regarding how Node.js handles rename events across file systems. File systems used by Linux and MacOS use inodes, and Node.js will attach file watchers to inodes rather than file paths. TypeScript tries to use the same watcher objects if it detects a path still exists on disk, so would end up reusing the watcher object instead of installing a new watcher at the original location, and would watch for changes at what might be an irrelevant file.

TypeScript 4.8 handles these cases on inode systems properly and installs a new watcher.

Elsewhere in TypeScript 4.8:

  • Optimizations have been introduced to speed up scenarios around --watch and --incremental, along with project references using--build.
  • Inference from binding patterns has been improved.
  • When running find-all-references in an editor, TypeScript now is smarter in aggregating references. This reduced the amount of time needed to search a widely used identifier by about 20%.
  • In a breaking change, unconstrained generics are no longer assignable to {}. In another breaking change, types cannot be imported or exported in JavaScript files.

TypeScript 4.8 can be downloaded via NuGet, or via NPM using the following command:

npm install -D typescript

The production release follows a release candidate published on August 11 and a beta release published on June 21. TypeScript 4.8 follows the May 2022 release of TypeScript 4.7.

Copyright © 2022 IDG Communications, Inc.