News

TypeScript 4.7 Beta Tackles 'Very Difficult' ECMAScript Module Support

Microsoft shipped the beta release of TypeScript 4.7, which tackles a "very difficult feature," ECMAScript Module Support in Node.js.

The difficulty arises from two different JavaScript module systems used in tooling essential to TypeScript: ECMAScript modules (ESM) used in the official JavaScript standard, and CommonJS (CJS), used in Node.js, the popular, open source, cross-platform, back-end JavaScript runtime environment commonly used to execute JavaScript code outside a web browser.

Here's how the difficult problem was described by Daniel Rosenwasser, senior program manager, in an April 8 blog post:

For the last few years, Node.js has been working to support ECMAScript modules (ESM). This has been a very difficult feature, since 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; however, support for ESM in Node.js was largely implemented in Node.js 12 and later. Around TypeScript 4.5 we rolled out nightly-only support for ESM in Node.js to get some feedback from users and let library authors ready themselves for broader support.

To add this functionality via high-level features, TypeScript 4.7 introduces two new module settings: node12 and nodenext. They are used with a new Node.js setting called type in the package.json file that lets developers specify module or commonjs to control whether .js files are interpreted as ES modules or CommonJS modules, defaulting to CommonJS when not set.

Also, related functionality is provided for new file extensions, CommonJS interop, exports/imports and more.

Another new feature is more control over module detection, helping to address differences in how Node.js and TypeScript handle module entry points, differences between existing "script" code and new module code, import and export statements and more.

Some other highlights of TypeScript 4.7 beta include:

  • Object Method Snippet Completions: "TypeScript now provides snippet completions for object literal methods. When completing members in an object, TypeScript will provide a typical completion entry for just the name of a method, along with a separate completion entry for the full method definition!"
    Object Method Snippet Completions in Animated Action
    [Click on image for larger, animated GIF view.] Object Method Snippet Completions in Animated Action (source: Microsoft).
  • Control-Flow Analysis for Computed Properties: "TypeScript 4.7 now analyzes the type of computed properties and narrows them correctly."
  • Improved Function Inference in Objects and Methods: "TypeScript 4.7 can now perform more granular inferences from functions within objects and arrays. This allows the types of these functions to consistently flow in a left-to-right manner just like for plain arguments."
  • typeof on #private Fields: "TypeScript 4.7 now allows us to perform typeof queries on private fields."
  • Resolution Customization with moduleSuffixes: "TypeScript 4.7 now supports a moduleSuffixes option to customize how module specifiers are looked up."

The announcement post also lists several breaking changes and details many other new features.

Going forward, the dev team will work to get v4.7 ready for a Release Candidate that's scheduled for May 10, according to the TypeScript 4.7 Iteration Plan.

About the Author

David Ramel is an editor and writer for Converge360.

comments powered by Disqus

Featured

  • Creating Reactive Applications in .NET

    In modern applications, data is being retrieved in asynchronous, real-time streams, as traditional pull requests where the clients asks for data from the server are becoming a thing of the past.

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

Subscribe on YouTube