Gaspard Bucher
Gaspard Bucher

Reputation: 6137

How to declare an external library acting like a class in typescript

I tried to create the following d.ts file but the type for the created element is any:

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

When using it:

import * as JSZip from 'jszip'

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

What is the proper way to do this ?

Upvotes: 5

Views: 1986

Answers (1)

Guria
Guria

Reputation: 7133

You must declare your module as interface with constructor declaration.

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
}

enter image description here

Upvotes: 7

Related Questions