Categories
TypeScript Answers

How to import a JavaScript library without definition file in a TypeScript file?

Spread the love

Sometimes, we want to import a JavaScript library without definition file in a TypeScript file.

In this article, we’ll look at how to import a JavaScript library without definition file in a TypeScript file.

How to import a JavaScript library without definition file in a TypeScript file?

To import a JavaScript library without definition file in a TypeScript file, we can add the typed definition file for the module in our own project.

To do this, we add

declare module "lib/errorInfoHandler" {
  const noTypeInfoYet: any;
  export = noTypeInfoYet;
}

into a .d.ts file in our TypeScript project.

We add our own type definition for the lib/errorInfoHandler module.

In it, we add the members of the modules that we know exit with

const noTypeInfoYet: any;
export = noTypeInfoYet;

Conclusion

To import a JavaScript library without definition file in a TypeScript file, we can add the typed definition file for the module in our own project.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.