Categories
TypeScript Answers

How to declare third party modules with TypeScript?

Spread the love

Sometimes, we want to declare third party modules with TypeScript.

In this article, we’ll look at how to declare third party modules with TypeScript.

How to declare third party modules with TypeScript?

To declare third party modules with TypeScript, we use the declare keyword.

For instance, we write

declare module "foo-module" {
  function foo(): void;
  export = foo;
}

in a .d.ts file in our TypeScript project to declare the foo-module module and its members.

Then we can import the module without errors by writing

import * as foo from "foo-module";
foo();

in our code.

Conclusion

To declare third party modules with TypeScript, we use the declare keyword.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *