如何在typesript中使用JavaScript构造函数?

时间:2020-04-06 06:35:11

标签: javascript typescript

我尝试在Typescript项目中使用Javascript库。 在JS库中,我有一个类(pdfjs-dist),该类具有如下使用的构造函数:

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

我的问题是如何在 .d.ts 文件中定义PDFFindController,以便可以使用该构造函数? 我尝试过这样的方法:

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

但是到目前为止,我仍然以PDFFindController未定义结束,所以我不能使用构造函数。

2 个答案:

答案 0 :(得分:1)

您正在使用pdf.js。您应该使用npm时,可能已从CDN下载或添加了它。已经为它编写了typings,要安装所有内容,请运行:

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

然后在您的代码中:

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

一旦您执行了该操作,这些类型就会起作用(假设您的tsconfig中没有错误)。

答案 1 :(得分:0)

最简单的方法是将返回的类型标记为any。这意味着TS将跳过类型检查。

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

是否可以解决您的问题,或者您需要输入此类型?

相关问题