danang-id / simple-crypto-js

Simplified AES cryptography for safer and easier encryption and decryption processes of any JavaScript objects.
https://simplecrypto.js.org
MIT License
85 stars 27 forks source link

Type error when using Typescript #3

Closed Vezul closed 5 years ago

Vezul commented 6 years ago

Hi,

I recently tried to use your library for a project, but encountered issues while compiling with strict-mode on:

node_modules/simple-crypto-js/src/SimpleCrypto.ts:9:22 - error TS7006: Parameter 'secret' implicitly has an 'any' type.

9   public constructor(secret) {
                       ~~~~~~

node_modules/simple-crypto-js/src/SimpleCrypto.ts:23:11 - error TS2322: Type 'string | null' is not assignable to type 'string'.
  Type 'null' is not assignable to type 'string'.

23     const string: string = typeof data == "object" ? JSON.stringify(data) : typeof data == "string" || typeof data == "number" || typeof data == 'boolean' ? data.toString() : null;
             ~~~~~~

node_modules/simple-crypto-js/src/SimpleCrypto.ts:31:77 - error TS2345: Argument of type '{ iv: string | WordArray; padding: Padding; mode: Mode; }' is not assignable to parameter of type 'CipherOption'.
  Types of property 'iv' are incompatible.
    Type 'string | WordArray' is not assignable to type 'string | undefined'.
      Type 'WordArray' is not assignable to type 'string'.

 31     const encrypted: CryptoJS.WordArray = CryptoJS.AES.encrypt(string, key, {
                                                                                ~
 32       iv: initialVector,
    ~~~~~~~~~~~~~~~~~~~~~~~~
...
 34       mode: CryptoJS.mode.CBC
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 35     });
    ~~~~~

I have created a MR to fix these issues, and other small issues :)

danang-id commented 6 years ago

Yes, I had no time to add those typing before. Thanks for PR, but I have a note regarding the build.