如何在打字稿中声明一个像 class 一样的外部库
How to declare an external library acting like a class in typescript
我试图创建以下 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
执行此操作的正确方法是什么?
您必须将您的模块声明为带有构造函数声明的接口。
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
}
我试图创建以下 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
执行此操作的正确方法是什么?
您必须将您的模块声明为带有构造函数声明的接口。
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
}