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
- https://www.bennadel.com/blog/3169-adding-custom-typings-files-d-ts-in-an-angular-2-typescript-application.htm
- https://codedump.io/share/pMlcq1Kekxmz/1/typescript-how-to-define-custom-typings-for-installed-npm-package
- https://medium.com/@mweststrate/how-to-create-strongly-typed-npm-modules-1e1bda23a7f4#.9iog13xrm
- https://github.com/Microsoft/TypeScript/issues/11329
- https://forum.ionicframework.com/t/resolve-custom-typings-with-typescript-2-0/64946
- http://stackoverflow.com/questions/39850527/ionic-2-register-custom-typings-file-in-rc-0/39850788#39850788 (wrong?)
- http://www.davevoyles.com/2016/02/16/how-to-add-type-definitions-to-a-typescript-project/
Creating custom typings files
- http://peter.grman.at/how-to-write-typescript-definition-files/
- http://stackoverflow.com/questions/37641960/typescript-how-to-define-custom-typings-for-installed-npm-package
- http://definitelytyped.org/guides/best-practices.html
- https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html
- https://templecoding.com/blog/2016/03/31/creating-typescript-typings-for-existing-react-components/
Ambient Declarations of Typings
(seems like this term and usage are now obsolete)
- http://stackoverflow.com/questions/35953848/what-are-ambient-typings-in-the-typescript-typings-tool
- https://basarat.gitbooks.io/typescript/docs/types/ambient/intro.html
- https://github.com/typings/typings/issues/239
- http://blog.mgechev.com/2016/03/28/ambient-type-definitions-duplicate-identifier-typescript-fix/
Leave a Reply