New TypeScript 4.5 improves asynchronous programming – Visual Studio Magazine

New

New TypeScript 4.5 improves asynchronous programming

TypeScript 4.5 comes with a new Awaited type and Promise improvements to improve asynchronous programming in the popular version of Microsoft over JavaScript which adds statically checked types.

One thing v4.5 does not have is ECMAScript module support for Node.js 12, which has been deferred from beta. It will reappear in a future release, but for now it’s only an experimental feature in the nightly releases.

“It was not an easy decision, but our team had a combination of concerns about ecosystem readiness and general advice on how / when to use the feature,” said Daniel Rosenwasser, Senior Program Manager, in a statement. blog post from November 17th. “We thought it would be better to smooth out the user experience instead of posting something that would ultimately be too frustrating for most people. In the meantime, you can still use the new media for --module nodenext and --moduleResolution nodenext as experimental features in the nightly versions of TypeScript. If you try to use these settings in TypeScript 4.5, you will receive an error message asking you to use a nightly version instead. ”

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 useful for modeling existing APIs, including built-in JavaScript components such as Promise.all, Promise.race, etc. Rosenwasser said. “In fact, some of the problems with inference with Promise.all served as motivation for Awaited. ”

Other changes, as well as descriptions of Rosenwasser and links for more information, include:

  • Pattern String Types as Discriminants: TypeScript 4.5 can now restrict values ​​that have pattern string types and also recognizes pattern string types as discriminants. Learn more about the change enabling this functionality here.
  • New snippet completions: TypeScript 4.5 brings two new snippet completions – these are completions that add default text and allow developers to possibly step through pieces of code they want to tweak.
    Snippet Complements for Methods in Classes in Animated Action
    [Click on image for larger, animated GIF view.] Snippet Complements 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 high level await, which means developers can use await apart from async functions. This was already supported in --module esnext (and now --module nodenext), corn es2022 is the first stable target for this feature. More information on this is available here.
  • Presence checks for private fields: TypeScript 4.5 supports an ECMAScript proposal to check if an object has a private field on it. Developers can now write a class with a #private field member and see if another object has the same field using the in operator.
  • Import assertions: TypeScript 4.5 supports an ECMAScript proposal for import assertions. This is a syntax used by runtimes to ensure that an import has an expected format.
  • Const Assertions and Default Type Arguments in JSDoc: TypeScript 4.5 brings additional expressiveness to our JSDoc support.
  • Faster loading time with realPathSync.native: TypeScript now uses the realpathSync.native run in Node.js on all operating systems. Previously, this function was only used on Linux, but in TypeScript 4.5, as long as you are running a sufficiently recent version of Node.js, the compiler will also use the function on operating systems which are generally case insensitive, like Windows and MacOS. This change made the project load faster by 5 to 13% on some Windows code bases. More information on this is available here (original change) and here (specific changes to 4.5).
  • Better editor support for unresolved types: In some cases, editors will take advantage of a light “partial” semantic mode, either while the editor waits for the full project to load, or in contexts such as as GitHub’s web 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 was not found, TypeScript replaced it with any in quick information. In TypeScript 4.5, TypeScript will do its best to preserve what you’ve written. More information on this is available in this implementation.
  • Experimental Night-Only ECMAScript Module Support in Node.js: Over the past few years, Node.js has been working to support running ECMAScript Modules (ESMs). This has been a very difficult feature to support, as the foundation of the Node.js ecosystem is based on a different module system called CommonJS (CJS). Interoperability between the two brings great challenges, with many new features to juggle. TypeScript 4.5 initially added new parameters to support direct execution of ECMAScript modules in Node.js; however, we believe that the current experiment needs more “cooking time” before it can be used more widely. You can see more details on why here. In turn, this feature is still available, but only under nightly versions of TypeScript, not in TypeScript 4.5. Go here for more information on this.

“We are already working on TypeScript 4.6!” Rosenwasser concluded. “If you’re curious about more, you can check out Milestone 4.6 on GitHub until the iteration plan is released on TypeScript issue tracking. We currently intend to focus on performance. and stability in the next version. “

We have verified this milestone, and it would have been 11% complete, with no due date stated. Out of 84 open v4.6 items, the one with the most comments (34) is “Slow IntelliSense in project”, which is in “need investigation” status. Posted Oct 11, it says: “I am trying to use ctrl + load space … for a while, when I disabled it from the built-in extensions, the list shows as expected. ”

About the Author

David Ramel is editor and writer for Converge360.

About Jon Moses

Check Also

ShiftLeft to present at No Hat 2021 conference

Receive instant alerts for news on your actions. Claim your 1-week free trial for Street …