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

Typescript types definition missing for es5 build #12827

Closed
lanwin opened this issue Jan 7, 2021 · 4 comments · Fixed by #12834
Closed

Typescript types definition missing for es5 build #12827

lanwin opened this issue Jan 7, 2021 · 4 comments · Fixed by #12834
Labels

Comments

@lanwin
Copy link

lanwin commented Jan 7, 2021

The current type definition did only work if pdfjs is directly imported, but not if the es5 build is used (when used with node).

I had to add a d.ts file to make it work. But I think this should be directly included in the release.

declare module "pdfjs-dist/es5/build/pdf.js" {
  export * from "pdfjs-dist";
}
@timvandermeij
Copy link
Contributor

/cc @tamuratak @ineiti Could one of you perhaps comment here on how this works? I believe the existing types that you added/improved should be generally available if I'm not mistaken, but I'm not sure how to test this.

@tamuratak
Copy link
Contributor

tamuratak commented Jan 8, 2021

I think it is an easy solution to tell users write the following:

const pdfjsLib: typeof import('pdfjs-dist') = require('pdfjs-dist/es5/build/pdf.js');

See doc for typeof import.

If we want an out-of-the-box solution, it is not easy. The TypeScript compiler finds the type file types/pdf.d.ts for build/pdf.js based on the fields main and types of pdfjs-dist/package.json.

  "license": "Apache-2.0",
  "main": "build/pdf.js",
  "name": "pdfjs-dist",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/mozilla/pdfjs-dist.git"
  },
  "types": "types/pdf.d.ts",
  "version": "2.6.347"

Therefore, the TypeScript compiler cannot find the type file types/pdf.d.ts for pdfjs-dist/es5/build/pdf.js. Moreover, because we cannot put multiple files to the main field, microsoft/TypeScript/issues/21423, we have no way to tell the TypeScript compiler the location of the type file for pdfjs-dist/es5/build/pdf.js.

The only workaround we can have is putting a pdf.d.ts file in the same directory as pdfjs-dist/es5/build/pdf.js. The content of the pdf.d.ts file should be:

export * from 'pdfjs-dist'

@lanwin
Copy link
Author

lanwin commented Jan 8, 2021

I think the first approach can not work when ES Modules are used cause require is not allowed there.

My guess is that the second approach would be far better.

Tanks for looking into this!

@Snuffleupagus
Copy link
Collaborator

Snuffleupagus commented Jan 8, 2021

Given that this particular issue seem to be mostly related to building/packaging the PDF.js library, rather than TypeScript as such, I've submitted a small patch following the suggestion in #12827 (comment). @tamuratak Thanks for your help!


@lanwin Please keep in mind that the PDF.js library is still a JavaScript project; hence it's generally speaking going to be up to the TypeScript users themselves to help maintain/improve the definitions where necessary :-)

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

Successfully merging a pull request may close this issue.

4 participants