Improving TypeScript modules

By on September 29, 2016 3:15 pm

improving typescript modules

One of the main challenges with creating modern JavaScript web applications is the relatively incomplete approach to ES modules. As an interim solution until all necessary use cases are solved, many developers create source code with ESM, and then transpile to either AMD, CJS, or UMD for easy usage within today’s browsers. As we work on creating Dojo 2 in a manner that is easy to use for both TypeScript and JavaScript users, one challenge we faced was how to support loader plugins and/or other dynamic module loading. AMD has long supported loader plugin syntax, and SystemJS supports loader plugins as well as System.import.

In our proposal on Better support for loader plugins in TypeScript, we came to the conclusion that adding true wildcard support would solve many of the challenges we faced. This extension makes it easy to implement features such as AMD loader plugins for text resources and other non-module code. After a healthy amount of discussion and thought, the TypeScript team agreed that adding support for shorthand ambient module declarations and wildcard chars in module names was a good idea.

This addition solves many challenges for developers working with TypeScript, and we’re very happy to see this feature land in TypeScript 2. It makes things like this possible:

declare module "json!*" {
    let json: any;
    export default json;
}

import d from "json!a/b/bar.json";
// lookup:
//    json!a/b/bar.json
//    json!*

This is yet another simple example of how great the open source community process can work to improve not only Dojo 2, but any project that leverages TypeScript!


Learning more

Support Logo

Get help from SitePen Support, our fast and efficient solutions to JavaScript development problems of any size.

Workshops Logo

SitePen workshops are a fun, hands-on way to keep up with JavaScript development and testing best practices. Register for an online workshop, today!

Let's Talk! Logo

Let’s talk about how we can help your organization improve their approach to automated testing.

Contact Us Logo

Have a question? We’re here to help! Get in touch and let’s see how we can work together.