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.