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!

« Read the rest of this entry »

Where Am I?

You are currently browsing entries tagged with modules at iHack, therefore iBlog.