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

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:

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:

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

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

I’ve started seeing this error when I upgraded eslint to 7.4.0. And to make things even more complicated:

  1. It was only happening during the CI build. I tried to reproduce the issue locally on a “clean” environment and I couldn’t get it to fail.
  2. XYZ was not my direct dependency. My project has a dependency on “ABC” and “ABC” has a dependency on “XYZ”

It felt weird to install types for 3rd or n-party dependency and it didn’t make sense to me (I might be wrong though!).

Instead of ratholing on that, I added the following statement to thetsconfig.json file to disable the ‘any’ type errors:

Note that disabling this will disable the rule for your project as well. But at least your build will be working, until the n-th dependency fixes this issue in their codebase.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store