New TypeScript 4.5 improves asynchronous programming – Visual Studio Magazine


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 and when to use the feature,” said Daniel Rosenwasser, senior manager of the ecosystem. program, in a blog of November 17. Publish. “We figured 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 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:

  • Model string types as discriminants: TypeScript 4.5 can now restrict values ​​that have pattern string types and also recognizes pattern string types as discriminators. Learn more about the change activating this feature here.
  • New extract 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 on private land: 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 (the change of origin) and here (changes specific 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 entire project to load, or in contexts such 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 support for ECMAScript Nightly-Only module in Node.js: For the past few years, Node.js has been working to support running ECMAScript (ESM) modules. 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 night outings by 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 are curious to know more, you can consult the Milestone 4.6 on GitHub until the iteration plan is posted to TypeScript Issue Tracker. We currently intend to focus on performance and stability in the next release. “

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 “IntelliSense slow in the project“, which has the status” need investigation “. Posted on October 11, it says:” I tried to use ctrl + load space … for a while, when I turned it off at from the built-in extensions, the list appears as expected. ”

About the Author

David Ramel is editor and writer for Converge360.

Comments are closed.