TypeScript How to use a javascript library without a type definition file Make a module that exports a default any


Example

For more complicated projects, or in cases where you intend to gradually type a dependency, it may be cleaner to create a module.

Using JQuery (although it does have typings available) as an example:

// place in jquery.d.ts
declare let $: any;
export default $;

And then in any file in your project, you can import this definition with:

// some other .ts file
import $ from "jquery";

After this import, $ will be typed as any.

If the library has multiple top-level variables, export and import by name instead:

// place in jquery.d.ts
declare module "jquery" {
   let $: any;
   let jQuery: any;

   export { $ };
   export { jQuery };
}

You can then import and use both names:

// some other .ts file
import {$, jQuery} from "jquery";

$.doThing();
jQuery.doOtherThing();