New TypeScript 4.5 Improves Asynchronous Programming

TypeScript 4.5 has shipped with a new Awaited type and Promise improvements for enhancing asynchronous programming in Microsoft's popular take on JavaScript that adds statically checked types.

One thing v4.5 doesn't have is ECMAScript module support for Node.js 12, which has been deferred since the beta release. It will reappear in some future release, but for now it's only an experimental feature in nightly releases.

"This was not an easy decision, but our team had a combination of concerns around ecosystem readiness and general guidance for how/when to use the feature," said Daniel Rosenwasser, senior program manager, in a Nov. 17 blog post. "We felt it would be better to smooth out the user experience instead of releasing something that would ultimately be too frustrating for most people. In the meantime though, you can still use the new support for --module nodenext and --moduleResolution nodenext as experimental features in nightly builds of TypeScript. If you try to use these settings in TypeScript 4.5, you'll receive an error message directing you to use a nightly build instead."

The new Awaited type, meanwhile, was introduced to model operations such as await in async, or with Promises in the .then() method.

"The Awaited type can be helpful for modeling existing APIs, including JavaScript built-ins like Promise.all, Promise.race, etc.," Rosenwasser said. "In fact, some of the problems around inference with Promise.all served as motivations for Awaited."

Some other changes, along with descriptions by Rosenwasser and some links for more information, include:

  • Template String Types as Discriminants: TypeScript 4.5 now can narrow values that have template string types, and also recognizes template string types as discriminants. See more on the change enabling this feature here.
  • New Snippet Completions: TypeScript 4.5 brings two new snippet completions -- these are completions that add some default text and allow developers to possibly tab through bits and pieces of the code that they may want to adjust.
    Snippet Completions for Methods in Classes in Animated Action
    [Click on image for larger, animated GIF view.] Snippet Completions for Methods in Classes in Animated Action (source: Microsoft).
  • --module es2022: Thanks to Kagami S. Rosylight, TypeScript now supports a new module setting: es2022. The main feature in --module es2022 is top-level await, meaning developers can use await outside of async functions. This was already supported in --module esnext (and now --module nodenext), but es2022 is the first stable target for this feature. More on this is available here.
  • Private Field Presence Checks: TypeScript 4.5 supports an ECMAScript proposal for checking whether an object has a private field on it. Developers can now write a class with a #private field member and see whether another object has the same field by using the in operator.
  • Import Assertions: TypeScript 4.5 supports an ECMAScript proposal for import assertions. This is a syntax used by runtimes to make sure that an import has an expected format.
  • Const Assertions and Default Type Arguments in JSDoc: TypeScript 4.5 brings some extra expressivity to our JSDoc support.
  • Faster Load Time with realPathSync.native: TypeScript now leverages the realpathSync.native function in Node.js on all operating systems. Previously this function was only used on Linux, but in TypeScript 4.5, as long as you're running a recent-enough version of Node.js, the compiler will also use the function on operating systems that are typically case-insensitive, like Windows and MacOS. This change sped up project loading by 5-13 percent on certain codebases on Windows. More on this is available here (the original change) and here (4.5-specific changes).
  • Better Editor Support for Unresolved Types: In some cases, editors will leverage a lightweight "partial" semantic mode -- either while the editor is waiting for the full project to load, or in contexts like GitHub's web-based editor. In older versions of TypeScript, if the language service couldn't find a type, it would just print any. For example, if type Buffer wasn't found, TypeScript replaced it with any in quick info. In TypeScript 4.5, TypeScript will try its best to preserve what you wrote. More on that is available in this implementation.
  • Experimental Nightly-Only ECMAScript Module Support in Node.js: For the last few years, Node.js has been working to support running ECMAScript modules (ESM). This has been a very difficult feature to support, since the foundation of the Node.js ecosystem is built on a different module system called CommonJS (CJS). Interoperating between the two brings large challenges, with many new features to juggle. TypeScript 4.5 initially added new settings to support directly running ECMAScript modules in Node.js; however, we believe that the current experience needs more "bake time" before it can be used more broadly. You can see more details of why here. In turn, this feature is still available for use, but only under nightly releases of TypeScript, and not in TypeScript 4.5. Go here for more information on this.

"We're already working on TypeScript 4.6!" Rosenwasser concluded. "If you're curious to hear more, you can check out the 4.6 milestone on GitHub until the iteration plan is posted on the TypeScript issue tracker. We currently intend to focus on performance and stability in the next release."

We did check that milestone, and it's reportedly 11 percent finished, with no due date listed. Out of 84 open v4.6 items, the one with the most comments (34) is "Slow IntelliSense in project," which is at "needs investigation" status. Posted Oct. 11, it says: "trying to use ctrl + space loading... for a period of time when I disabled it from builtin extensions the list is shown as expected."

About the Author

David Ramel is an editor and writer for Converge360.

comments powered by Disqus


Subscribe on YouTube