Using External Modules in TypeScript Reply

Joseph DotsonWhen programming in TypeScript, you may need to use existing JavaScript modules. This should be a lot easier than it is. You should be able to just use a require and pass a flag in to let the compiler know that it’s a js library in order to convert it to an Any, however that is not possible.

What actually needs to be done is the creation of a .d.ts file. This file (or files) declares the shape of the module. This includes any functions or interfaces that might be needed used when using the class.

This can be an insanely benign process, however the http://definitelytyped.org/ guys have us covered. They collate tons of definitions files. Not only for common libraries people use like the entire node ecosystem, but also for small libraries like the cli-color library which lets you add color to your console output.

There are issues with some of the definitions files however. As an example, when using the Q library and calling resolve on a Deferral it isn’t required to pass a value. When using the definitions file this can cause a problem, however it’s a small enough problem that it’s easy enough to get around.

In the end, I would suggest using their library of definitions files. It may cause a couple of issues, but those issues pale in comparison to writing the files yourself.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s