首页 > 解决方案 > 如何在打字稿中使用 javascript 构造函数?

问题描述

我尝试在我的 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) {}

但到目前为止,我仍然PDFFindControllerundefined,所以我不能使用构造函数。

标签: javascripttypescript

解决方案


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

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

然后在您的代码中:

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

一旦你这样做,类型应该可以工作(假设你的 tsconfig 中没有错误)。


推荐阅读