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

  • AdaBoost Binary Classification Using C#

    Dr. James McCaffrey from Microsoft Research presents a C# program that illustrates using the AdaBoost algorithm to perform binary classification for spam detection. Compared to other classification algorithms, AdaBoost is powerful and works well with small datasets, but is sometimes susceptible to model overfitting.

  • From Core to Containers to Orchestration: Modernizing Your Azure Compute

    The cloud changed IT forever. And then containers changed the cloud. And then Kubernetes changed containers. And then microservices usurped monoliths, and so it goes in the cloudscape. Here's help to sort it all out.

  • The Well-Architected Architect on Azure

    In the dynamic field of cloud computing, the architect's role is increasingly pivotal as they must navigate a complex landscape, considering everything from the overarching architecture and individual service configurations to the various trade-offs involved. Here's help.

  • Windows Community Toolkit Update Improves Controls

    The Windows Community Toolkit advanced to version 8.1, adding new features, improving existing controls and making dependency changes.

Subscribe on YouTube