TypeScript 4.8 shines on intersection and union types

0
14
TypeScript 4.8 shines on intersection and union types

TypeScript 4.8, a planned upgrade to Microsoft’s strongly typed JavaScript variant, has reached the release candidate stage. The new version brings correctness and consistency improvements, along with fixes to file watching on Linux and macOS.

The TypeScript 4.8 release candidate (RC) arrived on August 11. The release is due for general availability on August 23. The RC adds support for excluding which files will be considered in auto imports.

The preceding beta release, which published June 21, introduced 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.

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

npm install -D [email protected]

TypeScript 4.8 follows the May 2022 release of TypeScript 4.7.

Go to Publisher: InfoWorld
Author: