How to declare third party modules with TypeScript?

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.