Pdf.js: Feature request: Add type definitions for Typescript and make it available on NPM.

Created on 22 Dec 2016  路  20Comments  路  Source: mozilla/pdf.js

I would like to request "type definitions" for Typescript for this library. It would be great to use this library in my Typescript projects.

Thanks.

1-other 2-feature

Most helpful comment

I tried using the typings from DefinitelyTyped but they are so bad it's laughable. (laugh so you don't cry)

These are the latest typings (they were moved): https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/pdfjs-dist/index.d.ts

Here's how you would use them:

// imports typings first
import { PDFJSStatic, PDFDocumentProxy, PDFPromise } from 'pdfjs-dist';

// then import the actual library using require() instead of import
let pdfjs: PDFJSStatic = require("pdfjs-dist");

// only then you can actually do something useful
pdfjs.getDocument('helloworld.pdf').then(function (pdf: PDFDocumentProxy ) {
  let pageNum = 1;
  renderPage(pdf, pageNum);
});

Do you see how you have to import an imaginary PDFJSStatic type and then initialize a variable of that type using require()? That's complete nonsense. (No offense to the original author)

Please reopen this issue because there is a need for better quality typings for pdfjs. Just take a look at a few of these SO questions:

I think that most people who try to use pdf.js in a TypeScript app end up writing crap code or just give up and try something else. It doesn't help that the typings were moved from @types/pdf to @types/pdfjs-dist without any sort of announcement from DefinitelyTyped.

So really, it would be MUCH better for TypeScript devs everywhere if the pdfjs-dist package had self-contained, high quality typings.

All 20 comments

Could you elaborate a bit about what this is exactly? Why can't you use it in TypeScript projects right now and how large is this change?

It's possible to re-create definition files from display/api.js, and it will be easier for person who already done that. We shall adapt style from https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html . A problem will be to keep api.js and definition files in sync but will be easier after the latter is created.

Notice that pdf.d.ts shall be created for the pdfjs-dist/build/pdf.js file and not files in the src/ folder.

I'm not sure how much work it is to make "type declaration files" because I have no experience myself creating "type declaration files".

I think you can use third party Javascript libraries in Typescript without "type declaration files", but when "type declaration files" are available, you can get rich intellisense while using third party libraries.

https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html is a good resource to start learning about "type declaration files".

I would prefer if you could also make the "type declaration files" available on NPM. It makes it easy to include them in a project.

For example by using to following command: npm install @types/pdfjs-dist --save-dev

Publishing them via "@types/" is one way to do it and usually made by third-party contributors. If we are committed to include support for .d.ts files, then they can be included in pdfjs-dist's package.json manifest (there not reason to place them into separate npm package afaik)

P.S. See https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html for the differences.

Apparently, you can bundle "type declaration files" together with your already existing NPM package. I forgot about that. No need to publish them to @types/

There a d.ts file in the DefinitelyTyped repo for this: DefinitelyTyped pdf.js

I have not checked how complete or accurate it is, but it might be a good starting point.

I tried using the typings from DefinitelyTyped but they are so bad it's laughable. (laugh so you don't cry)

These are the latest typings (they were moved): https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/pdfjs-dist/index.d.ts

Here's how you would use them:

// imports typings first
import { PDFJSStatic, PDFDocumentProxy, PDFPromise } from 'pdfjs-dist';

// then import the actual library using require() instead of import
let pdfjs: PDFJSStatic = require("pdfjs-dist");

// only then you can actually do something useful
pdfjs.getDocument('helloworld.pdf').then(function (pdf: PDFDocumentProxy ) {
  let pageNum = 1;
  renderPage(pdf, pageNum);
});

Do you see how you have to import an imaginary PDFJSStatic type and then initialize a variable of that type using require()? That's complete nonsense. (No offense to the original author)

Please reopen this issue because there is a need for better quality typings for pdfjs. Just take a look at a few of these SO questions:

I think that most people who try to use pdf.js in a TypeScript app end up writing crap code or just give up and try something else. It doesn't help that the typings were moved from @types/pdf to @types/pdfjs-dist without any sort of announcement from DefinitelyTyped.

So really, it would be MUCH better for TypeScript devs everywhere if the pdfjs-dist package had self-contained, high quality typings.

@StevenLiekens is absolutely right. The type definition file @types/pdfjs-dist is a TRAGEDY. It doesn't have even GlobalWorkerOptions. Is it that hard for Mozilla to provide a type definition file for this project? We expect more from Mozilla.

Bumping this. @types/pdfjs-dist isn't even close to usable.

Struggling through @types/pdfjs-dist as well. None of the text layer rendering is in it. I've resorted to augmenting the module through declaration merging:

declare module "pdfjs-dist" {
    export interface TextLayerRenderTask extends PDFLoadingTask<void> { }
    export interface RenderTextLayerOptions {
        textContent: TextContent;
        viewport: PDFPageViewport;
        container: HTMLElement;
    }
    export function renderTextLayer(options: RenderTextLayerOptions): TextLayerRenderTask;
}

@SandyGifford yes... the pdf.js typescript types are very very stale it seems.

If I send in a PR can we get this officially supported? The API isn't that difficult.

This is really really really really needed. pdf.js is great/functional but the API is essentially undocumented. This REALLY needs to be rectified.

+1, we need proper type definitions!

Please +1 the top comment instead of replying so it's easier for us to gauge interest from the community. Moreover, work for this is in progress; see #11235 which landed two days ago and #11209 which landed six days ago, both to improve the JSDoc comments for better documentation and TypeScript definition generation.

Is somebody working on writing up a types declaration file right now?

10575 has been worked on again, and we've decided that that's the way we would like to provide the TypeScript definitions.

Fixed by pull request #12102. It took quite some time to converge to a solution that was both maintainable and tested, but thanks to hard work from community members it's done now. Future pdfjs-dist releases will start shipping the TypeScript definitions as generated from our JSDoc comments.

We are open for contributions from the community to improve our API documentation even further, not only for better API documentation but also for better types since they are both generated from the JSDoc comments in the files.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AlexP3 picture AlexP3  路  3Comments

brandonros picture brandonros  路  3Comments

smit-modi picture smit-modi  路  3Comments

liuzhen2008 picture liuzhen2008  路  4Comments

dmisdm picture dmisdm  路  3Comments