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.

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 *