【问题标题】:How to declare an external library acting like a class in typescript如何在打字稿中声明一个像类一样的外部库
【发布时间】:2016-10-09 16:53:42
【问题描述】:

我尝试创建以下 d.ts 文件,但创建的元素的类型是 any

declare module 'jszip' {
  interface JSZip {
    (): void
    file ( name: string, data: string, opts: any ): void
    folder ( name: string ): JSZip
  }
  const dummy: JSZip
  export = dummy
}

使用时:

import * as JSZip from 'jszip'

const zip = new JSZip ()
// zip type === any

这样做的正确方法是什么?

【问题讨论】:

    标签: typescript typescript-typings


    【解决方案1】:

    您必须将您的模块声明为带有构造函数声明的接口。

    declare module 'jszip' {
      interface JSZipInterface {
        (): void
        file ( name: string, data: string, opts: any ): void
        folder ( name: string ): JSZipInterface
      }
    
      interface JSZipConstructor {
        new (): JSZipInterface
      }
    
    
      const module: JSZipConstructor
      export = module
    }
    

    【讨论】:

      猜你喜欢
      • 2017-06-25
      • 2013-01-22
      • 2016-05-19
      • 1970-01-01
      • 2019-05-26
      • 2016-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多