Paul Krill
Editor at Large

TypeScript 4.8 shines on intersection and union types

news
Aug 29, 20222 mins

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.
Credit: David Lofink

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.

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