soliloquyy
soliloquyy

Reputation: 355

How to use a javascript constructor in typesript?

I try to use a Javascript library in my Typescript project. In JS library, I have a class (pdfjs-dist) which has a constructor used like this:

findController = new _pdf_find_controller.PDFFindController({
    linkService: pdfLinkService,
    eventBus: eventBus
});

The problem I have is to how to define PDFFindController in .d.ts file, so I can use that constructor? I have tried approach like that:

class PDFFindController {
        constructor(linkService: LinkService, eventBus: EventBus) {}

But so far, I still end up with PDFFindController being undefined, so I cannot use the constructor.

Upvotes: 0

Views: 110

Answers (2)

zamber
zamber

Reputation: 938

You're using pdf.js. You probably downloaded it or added it from a CDN when you should use npm. There are typings already written for it, to install everything, run:

npm install pdfjs-dist
npm install -D @types/pdfjs-dist

Then in your code:

import { ... } from 'pdfjs-dist';

Once you do that types should work (assuming that you don't have errors in your tsconfig).

Upvotes: 1

Jacques Ramsden
Jacques Ramsden

Reputation: 871

The easiest way would be to mark the returned type as any. Meaning that TS will skip the type checking.

let (findController as any) = new _pdf_find_controller.PDFFindController({
linkService: pdfLinkService,
eventBus: eventBus});

Does that solve your problem or do you require a type for this?

Upvotes: 0

Related Questions