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
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. ”
Awaited type, meanwhile, was introduced to model operations such as
async, or with
Promises in the
Promise.race, etc. Rosenwasser said. “In fact, some of the problems with inference with
Promise.all served as motivation for
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.
--module es2022: Thanks to Kagami S. Rosylight, TypeScript now supports a new
es2022. The main feature in
--module es2022is high level
await, which means developers can use
asyncfunctions. This was already supported in
--module esnext(and now
--module nodenext), corn
es2022is 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
#privatefield 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.nativerun 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 entire 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
Bufferwas not found, TypeScript replaced it with
anyin 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 for use, 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 the project”, which is in the “need investigation” status. Posted Oct 11, it says, “I’m trying to use ctrl + load space … for a while, when I disabled it from the built-in extensions, the list shows as expected. ”
David Ramel is editor and writer for Converge360.