We occasionally come across NPM modules without type definitions.
We want to use these in our Typescript project, but typescript barks at us when we try to import hello. Specifically we get this error:
Could not find a declaration file for module ‘untyped-npm-module’. ‘~/typescriptexperiment/untyped-npm-module/index.js’ implicitly has an ‘any’ type.
Try `npm install @types/untyped-npm-module` if it exists or add a new declaration (.d.ts) file containing `declare module ‘untyped-npm-module’;`ts(7016)
So we need to add a declaration file.
Approach
mkdir -p src/@types/untyped-npm-module
touch src/@types/untyped-npm-module/index.d.ts
Then within index.d.ts add:
declare module 'untyped-npm-module' {
export function hello(name: string): string;
}
And now it works: