我怎样才能得到TypeScript加载PDF.js NPM模块和@types绑定生成工作Node.JS代码的方式?

上下文

我正在尝试将PDF.JS导入到TypeScript项目中。 我正在使用pdfjs-dist的DefinitelyTyped 绑定 ,通过npm install @types/pdfjs-distnpm install pdfjs-dist

问题

我似乎无法得到TypeScript编译我的项目。 我使用直接从DefinitelyTypedtesting中复制的源代码。 这是我试图编译的简化(仅删除)代码(来自DefinitelyTyped的testing代码的确切副本也以相同的方式失败):

 import { PDFJSStatic } from 'pdfjs-dist'; var PDFJS: PDFJSStatic; PDFJS.getDocument('helloworld.pdf').then(console.log); 

TypeScript查找types声明模块,并认为PDFJSStatic的导入是有效的。 它不认为PDFJS是初始化的,但如果我在tsconfigclosuresstrict的代码编译,但它编译为:

 "use strict"; exports.__esModule = true; var PDFJS; PDFJS.getDocument('helloworld.pdf').then(console.log); 

这显然不起作用。 这不是将汇编语句编译成任何东西。

如何将PDF.JS导入到TypeScript项目中并通过@types/pdfjs-dist的声明文件将其编译为工作Node.JS代码?

我所试过的

我已经尝试过不同的import变化,无济于事。 切换到require也似乎没有帮助。

我已经validation了pdjs-dist依赖项和@types/pdfjs-dist依赖项的存在,更新以及可以直接从NodeJS(非TypeScript程序)使用。

我已经尝试过我的tsconfig module各种值。 他们有时会更改生成的代码,但是他们都不会将其更改为包含所需的导入。

我已经尝试在import行上添加/// <reference path="../node_modules/@types/pdfjs-dist/index.d.ts" /> 。 这并没有改变行为。

环境

tsc版本2.4.2,节点8.5和npm 5.3。 我在我的项目根目录中有以下tsconfig.json

 { "compilerOptions": { "allowJs":true, "rootDir": ".", "outDir": "dist", "moduleResolution": "node" }, "include": [ "src/**/*" ], "exclude": [ "**/*.spec.ts", "dist/**/*" ] } 

也许你可以使用require函数。

添加@types/node包,并在你的源代码的顶部写require('pdfjs-dist') 。 所以,你可以像下面那样修改你的代码。

现在,这个代码将工作。

 import { PDFJSStatic } from 'pdfjs-dist'; const PDFJS: PDFJSStatic = require('pdfjs-dist'); PDFJS.getDocument('helloworld.pdf').then(console.log); 

我认为@types/pdfjs-dist在实现上存在问题。

我也尝试了不同的方式。 对我来说,最可读的是这个:

 import * as pdfjslib from 'pdfjs-dist'; let PDFJS = pdfjslib.PDFJS; PDFJS.disableTextLayer = true; PDFJS.disableWorker = true;