entronad / crypto-es

A cryptography algorithms library
Other
272 stars 30 forks source link

Issue importing it on Typescript #1

Closed hc-danieltiziani closed 4 years ago

hc-danieltiziani commented 4 years ago

I can't seem to find how to import the modules to use in typescript:

I tried all formats but all givens an error:

import { MD5 } from 'crypto-es'; Errors: node_modules/crypto-es/lib/index.d.ts:19:21 - error TS2370: A rest parameter must be of an array type.

19 static create(...args?: Array): Base;


node_modules/crypto-es/lib/index.d.ts:634:18 - error TS2417: Class static side 'typeof PasswordBasedCipher' incorrectly extends base class static side 'typeof SerializableCipher'.
  Types of property 'encrypt' are incompatible.
    Type '(cipher?: Cipher | undefined, message?: string | WordArray | undefined, password?: string | undefined, cfg?: object | undefined) => CipherParams' is not assignable to type '(cipher?: Cipher | undefined, message?: string | WordArray | undefined, key?: WordArray | undefined, cfg?: object | undefined) => CipherParams'.
      Types of parameters 'password' and 'key' are incompatible.
        Type 'WordArray | undefined' is not assignable to type 'string | undefined'.
          Type 'WordArray' is not assignable to type 'string'.

import CryptoES from 'crypto-es';
Errors:
Same errors

Typescript version: Version 3.7.4

tsconfig.json
```
{
  "compilerOptions": {
    "allowJs": true,
    "lib": ["DOM"],
    "experimentalDecorators": true,
    "resolveJsonModule": true,
    "strict": true,
    "module": "commonjs",
    "esModuleInterop": true,
    "target": "es2017",
    "noImplicitAny": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "inlineSourceMap": false,
    "outDir": "dist",
    "baseUrl": ".",
    "typeRoots": ["src/application/types", "node_modules/@types"]
  },
  "include": [
    "src/**/*",
    "src/**/*.json"
  ]
}

```
entronad commented 4 years ago

@hc-danieltiziani

I have checked your problem, please make these changes:

  1. In Typescript projects you need to make overall import like import CryptoES from 'crypto-es';
  2. add this entry to your tsconfig.json's compilerOptions: "skipLibCheck": true,:
JasonHK commented 3 years ago

@hc-danieltiziani

I have checked your problem, please make these changes:

1. In Typescript projects you need to make overall import like `import CryptoES from 'crypto-es';`

2. add this entry to your tsconfig.json's compilerOptions: `"skipLibCheck": true,`:

@entronad You really should separate the TypeScript definition file...

entronad commented 3 years ago

@JasonHK You mean create .d.ts files for all js files in /lib dir?

Weeken commented 3 years ago

@JasonHK You mean create .d.ts files for all js files in /lib dir?

是的,不能将所有模块都包在一个namespace里面,这样没法treeshaking,都把代码打包进去了。 可以参考命名空间和模块这里改改

entronad commented 1 year ago

@JasonHK All js files have own d.ts now in v2.0.0