-1

I have imported crypto pouch in angular like below

import CryptoPouch from 'crypto-pouch';

But its showing error like below,

Could not find a declaration file for module 'crypto-pouch'. '/home/excercise_task/pouchDB/pouchApp/node_modules/crypto-pouch/index.js' implicitly has an 'any' type. Try npm i --save-dev @types/crypto-pouch if it exists or add a new declaration (.d.ts) file containing declare module 'crypto-pouch';ts(7016)

  • This is not an uncommon issue with TypeScript. As the compiler hints, this problem exists because there is no [ambient declaration](https://stackoverflow.com/questions/26946495/what-means-ambient-in-typescript) for that module, e.g. crypto-pouch.d.ts. There are a lot of workarounds. The professional way is the hero route: define the typings of crypto-pouch and submit that as a PR to the [crypto-pouch github project](https://github.com/calvinmetcalf/crypto-pouch). Otherwise search the web for `ts(7016)` for a suitable solution - there is no definitive answer other than providing a .d.ts. – RamblinRose Sep 21 '21 at 13:43

2 Answers2

0

Change import statement to require:

const CryptoPouch = require('crypto-pouch');

If you get could not find name require like the message below:

Cannot find name 'require'. Do you need to install type definitions for node?

Run:

npm i --save-dev @types/node
dom.macs
  • 776
  • 7
  • 15
0

Update
I ended up writing the type definition for crypto-pouch which merged at DefinitelyTyped, so just do this

$ npm install --save-dev @types/crypto-pouch

and don't use the work-around below


With respect to my comment on the OP, since [crypto-pouch](https://github.com/calvinmetcalf/crypto-pouch) only has two methods, and I've never written a pouchy plugin declaration, here's this. Just copy this content and place in your project, e.g. `src\crypto-pouch.d.ts` for an angular project. Intellisense should pickup this up and the ts(7016) should evaporate.
// extend PouchDB for the crypto-pouch plugin 

declare module "crypto-pouch"; // define the module for this definition

declare namespace PouchDB {
    namespace CryptoPouch {
        type Options = {
            /* A string password, used to encrypt documents. */
            password: string;
            /* (optional) Array of strings of properties that will not be encrypted. */
            ignore?: string[];
        };
    }
    /* Plugin */
    interface Database<Content extends {} = {}> {
        /**
         *
         * @param options See CryptoPouch.Options
         */
        crypto(options: CryptoPouch.Options): Promise<void>;
        /**
         *
         * @param password A string password, used to encrypt documents.
         * @param ignore Array of strings of properties that will not be encrypted.
         */
        crypto(password: string, ignore?: string[]): Promise<void>;
        /**
         * Disables encryption on the database and forgets your password.
         */
        removeCrypto(): void;
    }
}
RamblinRose
  • 4,883
  • 2
  • 21
  • 33