TypeScript 4.7 Beta Tackles “Very Difficult” ECMAScript Module Support – Visual Studio Magazine

News

TypeScript 4.7 Beta Tackles “Very Difficult” ECMAScript Module Support

Microsoft has released the beta version of TypeScript 4.7, which tackles a “very difficult feature”, support for the ECMAScript module in Node.js.

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

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

For a few years, Node.js has been working to support ECMAScript (ESM) modules. This was a very challenging feature, because the Node.js ecosystem is built on a different module system called CommonJS (CJS). Interoperability between the two brings great challenges, with many new features to juggle; however, ESM support in Node.js has been largely implemented in Node.js 12 and later. Around TypeScript 4.5, we rolled out nightly support only for ESM in Node.js to get user feedback and allow library authors to prepare 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 parameter called type in the package.json file which allows developers to 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 features are provided for new file extensions, CommonJS interoperability, exports/imports, etc.

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

Other highlights of the TypeScript 4.7 beta include:

  • Object method snippet completions: “TypeScript now provides snippet completions for object literal methods. When completing an object’s members, TypeScript will provide a typical completion entry for the name of a method only, as well as an entry for separate completion for full method definition!”
    Object method snippet completions in an animated action
    [Click on image for larger, animated GIF view.] Object method snippet completions in an animated action (source: Microsoft).
  • Control flow analysis for calculated properties: “TypeScript 4.7 now parses the type of computed properties and reduces them correctly.”
  • Improved function inference in objects and methods: “TypeScript 4.7 can now perform more granular inference from functions in objects and arrays. This allows the types of these functions to flow consistently from left to right, just like single arguments.”
  • typeof on #private The fields: “TypeScript 4.7 now allows us to perform type of queries on private fields.”
  • Customization of the resolution with moduleSuffixes: “TypeScript 4.7 now supports a moduleSuffixes option to customize how module specifiers are searched.”

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

Going forward, the development team will work to prepare 4.7 for a release candidate scheduled for May 10, according to the TypeScript 4.7 iteration plan.

About the Author


David Ramel is an editor and writer for Converge360.



Briana R. Cross