Home > Mobile >  crypto-pouch showing import error in angular
crypto-pouch showing import error in angular

Time:09-22

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)

CodePudding user response:

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

CodePudding user response:

With respect to my comment on the OP, since 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?: Array<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?: Array<string>): Promise<void>;
        /**
         * Disables encryption on the database and forgets your password.
         */
        removeCrypto(): void;
    }
}

If I have time I'll create a PR at the project's github repo.

  • Related