TypeScript 5.8 improves interoperability between CommonJS and ECMAScript

The new release allows CommonJS modules to be called from ECMAScript and provides a more detailed check of return expressions.

Save to Pocket listen Print view
Typewriter, keyboard, keyboard, TypeScript, Typewriter

(Image: erstellt mit KI durch iX)

3 min. read

Microsoft has released TypeScript 5.8. The new version of the programming language brings improved interoperability of CommonJS modules with the newer ECMAScript modules, which should simplify the work of library developers, as well as granular checks for branches in return expressions. However, not all features from the beta phase have been included in the final release.

As the development team explains, it is postponing a change relating to the checking of functions with conditional return types, which was still included in the beta, to the next TypeScript release. However, this work resulted in granular checks for branches within return expressions, which have made it into the current 5.8 release.

The TypeScript team uses an example to show what the new feature is all about. The following code is used to retrieve a URL object from a cache if it exists, or to create a new URL object if it does not yet exist. However, the code has the bug that the creation of a new URL object is missing in the input. Until now, TypeScript could not recognize this type of error. This now changes with version 5.8, which checks each branch of the conditional expression against the declared return type of the contained functions, if present.

Before TypeScript 5.8, the error was not recognized in the code:

declare const untypedCache: Map<any, any>;

function getUrlObject(urlString: string): URL {
    return untypedCache.has(urlString) ?
        untypedCache.get(urlString) :
        urlString;
}

TypeScript 5.8, on the other hand, issues an error message:

declare const untypedCache: Map<any, any>;

function getUrlObject(urlString: string): URL {
    return untypedCache.has(urlString) ?
        untypedCache.get(urlString) :
        urlString;
    //  ~~~~~~~~~
    // error! Type 'string' is not assignable to type 'URL'.
}

Node.js has been able to handle ECMAScript modules alongside CommonJS modules for years, but this had some pitfalls: ECMAScript files could support the import of CommonJS files, but a require from CommonJS files to ECMAScript files was not possible. This means that it was possible to work with CommonJS files from ECMAScript files, but not the other way around.

Library authors who wanted to use ECMAScript therefore had to either break compatibility with CommonJS or resort to a "dual publish" of their libraries, which, according to the TypeScript team, is a complex and error-prone process that roughly doubles the amount of code within a package.

Since Node.js 22, it has been possible to call require("esm") from CommonJS modules to ECMAScipt modules. It should be noted that a require() to ECMAScript files that contain a top-level await is still not permitted. TypeScript can handle this new behavior using the --module nodenext flag to provide developers with improved interoperability. Further information can be found in the pull request on GitHub.

All further details on TypeScript 5.8 can be found in a blog entry. Interested parties can follow the progress of the next version of TypeScript 5.9 in the issue tracker. Specific target dates and further details should be available there soon. Nightly builds for an early insight into version 5.9 can be installed via npm (npm install typescript@next) or Visual Studio code extension.

JavaScript conference by Heise: enterJS 2025
Enterprise JavaScript Conference enterJS 2025, May 7 and 8 in Mannheim, Germany

(Image: WD Ashari/Shutterstock.com)

enterJS 2025 will take place on May 7 and 8 in Mannheim. The conference offers a comprehensive look at the JavaScript-supported enterprise world. The focus is not only on the programming languages JavaScript and TypeScript themselves, but also on frameworks and tools, accessibility, practical reports, UI/UX and security.

Highlights from the program:

Tickets are available at an early bird price in the online store.

(mai)

Don't miss any news – follow us on Facebook, LinkedIn or Mastodon.

This article was originally published in German. It was translated with technical assistance and editorially reviewed before publication.