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 at Converge 360.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube