kulaka kulaka - 2 months ago 7
TypeScript Question

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?

Answer

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"

Comments