Hopding / pdf-lib

Create and modify PDF documents in any JavaScript environment
https://pdf-lib.js.org
MIT License
6.78k stars 647 forks source link

Is the a TypeScript type definition available? #558

Closed reselbob closed 4 years ago

reselbob commented 4 years ago

Something on the order of "@types/pdf-lib".

If not, as embarrassed as I am to ask, how can I get VS Code to recognize the types in this excellent library?

Hopding commented 4 years ago

Hello @reselbob!

The TypeScript types are included in the pdf-lib NPM package. You do not need to install anything additional to get them. VSCode should just pick them up. If it’s not, the only cause I can think of is that you have plugins or configurations preventing it from doing so (e.g. the Deno plugin might cause this - haven’t tested it myself though).

reselbob commented 4 years ago

Thank you for your prompt response. Everything seems be OK on this end.

I am using your project in my demonstration of the strategy pattern here: https://github.com/reselbob/designpatterns

Hopding commented 4 years ago

Are you sure you've setup/configured TypeScript correctly for your project? It's unlikely to be an issue on pdf-lib's end, as pdf-lib has thousands of users, and this is the first I've heard of the types being unavailable. If you're still having trouble, please reopen this issue with some additional diagnostic info and error messages.

Hopding commented 4 years ago

And, to be clear, it's possible there's an issue on pdf-lib's end. It just surprising that if there was one, it hasn't been reported until now. And I don't have any more information to go off of right now.

zackdotcomputer commented 3 years ago

Popping in here because I am seeing the same thing - running a small proof of concept in Deno with the library downloaded from skypack.dev and I'm not getting any typedefs picked up by VSCode.

For what it's worth, I also dug into the JS files linked to from https://cdn.skypack.dev/pdf-lib@%5E1.6.0 and there don't appear to be any TS typedefs in there? I am a Deno noob, though, so IDK if they're supposed to be somewhere else at a predictable (and so unlinked) location.

zackdotcomputer commented 3 years ago

OK having seen the conversation in #484 as well, I tried downgrading to the [deprecated] deno VSCode extension and it still did not work for me. I was still seeing the PDFDocument.load function have the type signature PDFDocument.load(pdf: any, options: any): any.

At this point, I was frustrated because I could see from the npm download that it should be working, so I spent too much time digging into this this morning.

Finally, I found a little note at the bottom of this page in Skypack's (formerly Pika) documentation that you have to add ?dts at the end of a skypack/pika import to get the TS definitions for deno sent alongside the code. Adding that to the import solved the issue even with the latest, official plugin.

TDLR; import { PDFDocument } from 'https://cdn.skypack.dev/pdf-lib@^1.11.1?dts'; works

Opened PR #621 to update Pika to Skypack, update the version to latest, and add the ?dts flag for typing in the README to save future devs having to find this thread.