在 pdfjs-dist (PDFJS) 中使用 TypeScript (TSX) 中的方法导入命名空间和接口 [英] Importing namespace and interface with methods in TypeScript (TSX) in pdfjs-dist (PDFJS)

查看:67
本文介绍了在 pdfjs-dist (PDFJS) 中使用 TypeScript (TSX) 中的方法导入命名空间和接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 React 项目中使用 pdfjs-dist,但是在尝试导入项目中的模块和函数时遇到很多问题.

I am trying to use pdfjs-dist in my React project, but get a lot of problems trying to import the module and the functions in the project.

@types/node_modules 中的 pdfjs-dist 模块 index.d.ts 被定义为包含一个命名空间PDF"和一个导出PDF"的模块pdfjs-dist".

The pdfjs-dist module index.d.ts in @types/node_modules is defined so that it contains a namespace "PDF" and a module "pdfjs-dist" which exports "PDF".

该文件具有接口,其中包含我想从其他类调用的getDocument(name:string)"等方法.

The file has interfaces, which contains methods such as "getDocument(name:string)" which I want to call from my other classes.

简而言之;该文件由很多通过该接口实现的接口和方法组成,形式为:

In short; the file consists of a lot of interfaces and methods that are implemented through this interface, on the form:

declare module "pdfjs-dist" {
  export = PDF;
}

declare namespace PDF {
  interface PDFJSStatic {
    getDocument(
      source: string,
      pdfDataRangeTransport ? : any,
      passwordCallback ? : (fn: (password: string) => void, reason: string) => string,
      progressCallback ? : (progressData: PDFProgressData) => void): PDFPromise < PDFDocumentProxy > ;

  }

我曾尝试使用常规的导入语句,例如:

I have tried to use the regular import statements, such as:

import * as PDF from "pdfjs-dist"

import { PDFJSStatic } from "pdfjs-dist"

但是,它似乎没有很好地响应.VS Code 给了我所有的接口,所以我可以看到它们是什么,但这就是我对 React 和 Typescript 的了解有点不足的地方.我将如何调用这些方法并实际使用getDocument()"方法?

However, it does not seem to respond very well. VS Code gives me all the interfaces, so I can see what they are, but this is where my knowledge of React and Typescript falls a bit short. How would I go about calling the methods and actually using the "getDocument()" method?

推荐答案

出于某种原因,修复似乎是先导入接口,以便在使用第 2 行的 require 语句时可以使用 PDFJSStatic 和其他接口.

For some reason the fix seems to be to import the interface first, so that the PDFJSStatic and other interfaces are available when using the require statement on line 2.

我使用的导入语句是;

import { PDFJSStatic, PDFPageProxy } from "pdfjs-dist";
let PDFJS: PDFJSStatic = require("pdfjs-dist");

这可能不是正确的做法,但确实有效.

This is probably not the correct way of doing it, but it works.

这篇关于在 pdfjs-dist (PDFJS) 中使用 TypeScript (TSX) 中的方法导入命名空间和接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆