kulaka
kulaka

Reputation: 33

TypeScript writing d.ts file for module that can be instantiate

I having problem creating declaration file for an existing module.

When using javascript the module imported using syntax:

var Library = require('thirdpartylibs');
var libInstance = new Library();

i have created d.ts file called thirdpartylibs.d.ts and add the following:

declare module 'thirdpartylibs'{
   export class Library{}
}

in my index.ts file :

import * as Library from 'thirdpartylibs'
let libInstance = new Library() // <--- error here

seems like i should do let libInstance = new Library.Library() to make it work, but it will fail on the generated JS.

anyone?

Upvotes: 3

Views: 791

Answers (1)

Bruno Grieder
Bruno Grieder

Reputation: 29884

Try this

declare module 'thirdpartylibs'{
   class Library {
      ...
   }
   export = Library
}

To import

import Library = require('thirdpartylibs')
const libInstance = new Library()

Note: this makes Library the export. The syntax you were using is a "named export"

Upvotes: 6

Related Questions