How to fix error TS7016: Could not find a declaration file for module ‘XYZ’. ‘file.js’ implicitly has an ‘any’ type

Peter Jausovec
2 min readJul 15, 2020
Photo by Jason Leung on Unsplash

If you’re using TypeScript you might have run into this error before. The second part of the error says:

Try `npm install @types/XYZ` if it exists or add a new declaration (.d.ts) file containing `declare module 'XYZ';

If XYZ is a direct dependency of your project, you can follow the instructions from the error message and run npm install @types/XYZ.

Sometimes the types might not be available and the npm install command fails. In that case, you can use the second option and declare the module.

Here’s how I usually do that.

  1. Create a folder called typings
  2. Create a file in that folder called index.d.ts
  3. Declare the module(s) like this:
declare module 'XYZ';

Lastly, you also need to add the path to your index.d.ts in the tsconfig.json file under the typeRoots element, like this:

"typeRoots": [
"./typings",
"./node_modules/@types/"
]

But ‘XYZ’ is not a direct dependency of my project

--

--

Responses (9)