question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Please include TypeScript types in the package itself

See original GitHub issue

Before you start - checklist

  • I understand that React-PDF does not aim to be a fully-fledged PDF viewer and is only a tool to make one
  • I have checked if this feature request is not already reported

Description

Currently TypeScript types are provided by an external @types/react-pdf package. This allows the types definition to get out of sync with the react-pdf library.

Normally this wouldn’t be an issue, except currently a mistake in the types package actually breaks react-pdf itself.

  • react-pdf uses "pdfjs-dist": "2.9.359",
  • @types/react-pdf uses "pdfjs-dist": "^2.10.377"

As a result it’s possible that node_modules/pdfjs-dist may be v2.10.377 while react-pdf uses v2.9.359. Because all of the PDF worker setup methods documented by react-pdf use node_modules/pdfjs-dist, this means it’s possible that simply installing @types/react-pdf can result in the pdfjs worker installed and the pdfjs library react-pdf uses being of different versions.

Proposed solution

Including the TypeScript types as part of the react-pdf package itself will permanently fix this issue by avoiding the duplication of the very specific dependency version.

  • A) The .d.ts files from the @types/react-pdf package can be moved to react-pdf’s codebase and maintained alongside it
  • B) Using those .d.ts files, react-pdf could be refactored to TypeScript .tsx files so react-pdf is written in TypeSript.

Alternatives

Yes, it is possible to fix the @types/react-pdf package for the current version (though the publishing method means this will likely take several days or even a week.

However even if we fix it now. Because react-pdf pins an exact version of pdfjs the next time react-pdf updates its version of pdfjs (which is already outdated) this bug will regress and will need to be fixed again. Though this will likely not happen in a timely manor as someone will have to discover and re-investigate this bug (which only shows up at runtime) before submitting a PR to update the pdfjs version in the types package to match.


One other possibility would be to use peerDeps instead of deps for the pdfjs package. This would ensure that pdfjs is installed at the root, with the correct version, and it matches the version that react-pdf expects.

Additional information

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
saadqcommented, Dec 18, 2021

For anyone else who is having issues due to being unable to import the PDFDocumentProxy type, you can just use a workaround like:

import { Document, DocumentProps } from 'react-pdf'

type LoadCallback = Required<DocumentProps>['onLoadSuccess']

function Display() {
  const onLoad: LoadCallback = useCallback((pdf) => {
    console.log(pdf.numPages) // `pdf` is properly typed here
    ...
  }, [])

  return (
    <Document loading="" file={url} onLoadSuccess={onLoad}>
      ...
    </Document>
  )
}
3reactions
wojtekmajcommented, Dec 13, 2021

Note: v5.6.0 is now released, with pdfjs-dist bumped to 2.10.377.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use types declaration on another package - Stack Overflow
In my project, is it possible to redeclare the source package types for the fork, so that the above example will work? typescript...
Read more >
Typescript Typings: The Complete Guide: @types Compiler ...
There are a ton of type definitions available that come bundled with the compiler, including for example all the type definitions of ES6...
Read more >
Documentation - Module Resolution - TypeScript
Please see the Modules documentation for more information. Module resolution is the process the compiler uses to figure out what an import refers...
Read more >
A quick introduction to “Type Declaration” files and adding ...
In this lesson, we are going to take a closer look at type declaration files which are one of the key ingredients of...
Read more >
How to Install Types for Packages -- newline - Fullstack.io
Not all of them are written in TypeScript, but many provide type ... You installed the package itself and try to import it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found