javascriptnode.jstypescriptnpmpaytm

TypeScript | Facing Error while trying to import a dependency


I am not used to using node js and typescript.

I tried importing Paytm dependency using the following code: npm install paytmchecksum or by adding the below code in package.json

"dependencies": {
    ...
    "paytmchecksum": "^1.5.0"
    ...
}

Now, I am using firebase, typescript and node.js. When I try importing, using

import PaytmChecksum from "../node_modules/PaytmChecksum";
import PaytmChecksum from "paytmchecksum";

or any other thing. There are just errors. I believe maybe because the dependency is to be used with javascript rather than typescript.

I get the following error:

Could not find a declaration file for module '../node_modules/PaytmChecksum'. 'd:/firebase-functions/functions/node_modules/PaytmChecksum/PaytmChecksum.js' implicitly has an 'any' type.

sometimes I get this,

Could not find a declaration file for module 'paytmchecksum'. 'D:/firebase-functions/functions/node_modules/paytmchecksum/PaytmChecksum.js' implicitly has an 'any' type. Try npm i --save-dev @types/paytmchecksum if it exists or add a new declaration (.d.ts) file containing declare module 'paytmchecksum';

What is the workaround for this problem?


Solution

  • You either need to create your own type declarations file or use @ts-ignore on the line before the import.

    If you create your own type.d.ts file, you will need to make sure that it is included the project in your tsconfig.json project file.

    The type file can be as simple as one line (./mytypes/paytmchecksum.d.ts):

    declare module 'paytmchecksum';
    

    This simply gets rid of the implied any and makes it explicit. You won't get any intellisense or type checking, but it will fix the error. You could go the extra mile and create a full type-definition file. If you, do you should add it to the @types repository so others can use it.

    Then just add include to your tsconfig.json file:

    { "include": [
      "mytypes/paytmchecksum.d.ts"
      ]
    }