Could not find a declaration file for module abc implicitly has an any type

September 02, 2022

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

 Could not find a declaration file for module abc implicitly has an any type

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

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

Sometimes the types might not be available and the npm installcommand 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 types under src folder e.g src/types
  2. Create a file in that folder called index.d.ts
  3. Declare the module(s) like this:
declare module 'BitFrit';
declare module 'react-native-dots-pagination';
declare module '@env' {
    export const ENV_VAR: string;
    export const API_URL: string;
} 

Lastly, you also need to add the path to yourindex.d.tsin the tsconfig.jsonfile under the typeRootselement, like this:

  "typeRoots": [
      "./src/types"
    ],  

Written by Manoj Bhardwaj who lives and works in Dharamshala Himachal Pradesh (India). My stackoverflow