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

Subscribe on YouTube