Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Add type definitions for Typescript and make it available on NPM. #7909

Closed
DannyBoyNg opened this issue Dec 22, 2016 · 23 comments

Comments

@DannyBoyNg
Copy link

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.

@timvandermeij
Copy link
Contributor

timvandermeij commented Dec 22, 2016

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?

@yurydelendik
Copy link
Contributor

yurydelendik commented Dec 22, 2016

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.

@yurydelendik
Copy link
Contributor

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

@DannyBoyNg
Copy link
Author

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".

@DannyBoyNg
Copy link
Author

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

@yurydelendik
Copy link
Contributor

yurydelendik commented Dec 22, 2016

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.

@DannyBoyNg
Copy link
Author

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/

@dmiller9911
Copy link

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.

@sliekens
Copy link

sliekens commented Jan 16, 2018

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.

@P0oOOOo0YA
Copy link

P0oOOOo0YA commented Nov 3, 2018

@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.

@therealparmesh
Copy link

therealparmesh commented Feb 11, 2019

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

@SandyGifford
Copy link

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;
}

@burtonator
Copy link

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

@burtonator
Copy link

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

@burtonator
Copy link

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

@soadzoor
Copy link

+1, we need proper type definitions!

@timvandermeij
Copy link
Contributor

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.

@geopic
Copy link

geopic commented Jan 19, 2020

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

@timvandermeij
Copy link
Contributor

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

@timvandermeij
Copy link
Contributor

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.

@LuisAlejandro
Copy link

Where are the instructions to use the typings provided by pdfjs-dist? I cant seem to find them anywhere

@hey24sheep
Copy link

@LuisAlejandro I am struggling with the same here. I can't declare the type for const pdfjs: ???? = require('path/pdf.js')

maifeeulasad added a commit to maifeeulasad/resume that referenced this issue Apr 23, 2023
@koogle
Copy link

koogle commented May 12, 2023

My suggestion is

import * as pdfjsLib from "pdfjs-dist";
import * as pdfjsViewer from "pdfjs-dist/web/pdf_viewer";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests