0

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.

1
  • How do you import or include the external js library? Commented Apr 6, 2020 at 6:40

2 Answers 2

1

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).

Sign up to request clarification or add additional context in comments.

Comments

0

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?

2 Comments

Using any is the easiest way, perhaps, but almost never the best way.
@JohnMontgomery I agree 100% with the fact that it's not the best way. That being said my suggestion was just so progression could be made. But yes ultimately solving the problem and not the symptom would be the better approach

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.