TypeScript 2: Adding custom.d.ts Typings Files for Existing JavaScript Modules

March 22, 2017 § Leave a comment

If you are trying to import ordinary JavaScript modules into a TypeScript project, and those modules don’t already have an internal or external typings file, you may get an error like:

  • error TS2688: Cannot find type definition file for ‘lodash’
  • error TS7016: Could not find a declaration file for module ‘himalaya’

For some reason, the TypeScript documentation talks a lot about how to create typings files for modules that don’t have them, but not about how to add them to your project. And it is incredibly difficult to Google the correct answer.

The best answer I found is from: https://www.bennadel.com/blog/3169-adding-custom-typings-files-d-ts-in-an-angular-2-typescript-application.htm

The actual answer is quite simple: are only four steps:

 $ npm install -g --save typings # if you haven't already
 $ cat > custom.d.ts # type definition file for that module
 declare module 'custom';
 ^D
 $ typings install --global --save file:custom.d.ts

That’s it!

References

Adding custom typing files

Creating custom typings files

Ambient Declarations of Typings

(seems like this term and usage are now obsolete)

Advertisements

Tagged: , , , ,

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading TypeScript 2: Adding custom.d.ts Typings Files for Existing JavaScript Modules at iHack, therefore iBlog.

meta

%d bloggers like this: