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

  • VS Code Copilot Previews New GPT-4o AI Code Completion Model

    The 4o upgrade includes additional training on more than 275,000 high-quality public repositories in over 30 popular programming languages, said Microsoft-owned GitHub, which created the original "AI pair programmer" years ago.

  • Microsoft's Rust Embrace Continues with Azure SDK Beta

    "Rust's strong type system and ownership model help prevent common programming errors such as null pointer dereferencing and buffer overflows, leading to more secure and stable code."

  • Xcode IDE from Microsoft Archrival Apple Gets Copilot AI

    Just after expanding the reach of its Copilot AI coding assistant to the open-source Eclipse IDE, Microsoft showcased how it's going even further, providing details about a preview version for the Xcode IDE from archrival Apple.

  • Introduction to .NET Aspire

    Two Microsoft experts will present on the cloud-native application stack designed to simplify the development of distributed systems in .NET at the Visual Studio Live! developer conference coming to Las Vegas next month.

  • Microsoft Previews Copilot AI for Open-Source Eclipse IDE

    Catering to Java jockeys, Microsoft is yet again expanding the sprawling reach of its Copilot-branded AI assistants, previewing a coding tool for the open-source Eclipse IDE.

Subscribe on YouTube

Upcoming Training Events