Skip to content

TypeScript

TypeScript 的类型系统、接口、泛型等内容

在 npm 包或 UMD 库中扩展全局变量

详细文档 TypeScript

使用 declare global 可以在 npm 包或者 UMD 库的声明文件中扩展全局变量的类型

ts
declare global {
  interface JSEncrypt {
    setPublicKey(publicKey: string): void
    setPrivateKey(privateKey: string): void
    encrypt(value: string): string
    decrypt(value: string): string
    getPublicKey(): string
    getPrivateKey(): string
  }

  interface JSEncryptConstructor {
    new(): JSEncrypt
  }

  const JSEncrypt: JSEncryptConstructor

}

export {}
ts
const jsencrypt = new JSEncrypt()
jsencrypt.setPublicKey(key)
return jsencrypt.encrypt(msg)

基于 MIT 许可发布