html2pdf.js icon indicating copy to clipboard operation
html2pdf.js copied to clipboard

Add support of typescript

Open umidjon-2231 opened this issue 11 months ago • 2 comments

This pull request introduces TypeScript definitions (type.d.ts) for html2pdf.js, allowing developers to use the library with improved type safety and better IDE support.

Changes Included:

  • Created a TypeScript declaration file (type.d.ts) with comprehensive type definitions for the library.
  • Added typings for Html2PdfOptions, Html2PdfWorker, and Html2PdfStatic.
  • Ensured all documented methods and parameters are typed correctly.
  • Included JSDoc comments for better documentation.

Motivation & Benefits:

  • Enables TypeScript developers to use html2pdf.js with type checking.
  • Enhances code editor autocompletion and documentation.
  • Reduces potential runtime errors by enforcing correct API usage.

How to Test:

  1. Import the library in a TypeScript project:
    import html2pdf from "html2pdf.js";
    
  2. Use the library with the new type definitions:
    const element = document.getElementById("element-to-print");
    html2pdf().from(element).set({ filename: "test.pdf" }).save();
    
  3. Verify that TypeScript recognizes the method signatures and shows relevant hints in an IDE.

Looking forward to feedback and suggestions!

umidjon-2231 avatar Mar 05 '25 16:03 umidjon-2231

Would love to see this merged! Thanks for your work :)

It seems like the maintainers of this library are busy working on bug fixes. You could consider adding this to DefinitelyTyped!

While waiting for types to be added, I forked your repo and installed it in my project(s) with npm install git+https://github.com/R1D3R175/html2pdf.js. If anyone wants to have types I encourage to either use PR's author fork or create its own fork and then install it via their dependency manager of choice that supports installing via git.

NOTE: I had to move type.d.ts to dist/ and add "types": "dist/type.d.ts" to package.json in order to make TypeScript find the .d.ts file.

R1D3R175 avatar Apr 07 '25 00:04 R1D3R175

@R1D3R175 Thanks for the feedback and for sharing your solution for using the types in your project! Moving the file and updating package.json is good to know.

umidjon-2231 avatar Apr 08 '25 18:04 umidjon-2231

@R1D3R175 Thanks for the feedback and for sharing your solution for using the types in your project! Moving the file and updating package.json is good to know.

I think if you add "types" to the package.json file as below in your PR @umidjon-2231 it will be fine:

{
  "name": "html2pdf.js",
  "version": "0.10.3",
  "description": "Client-side HTML-to-PDF rendering using pure JS",
  "main": "dist/html2pdf.js",
  "types": "type.d.ts",
  ...

j-d-carmichael avatar Aug 18 '25 20:08 j-d-carmichael

:tada: This PR is included in version 0.11.3 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

This is amazing, thank you so much @umidjon-2231 ! Sorry there's been radio silence for the past several months, I haven't had loads of time for this project. I really appreciate this contribution, will add the types to package.json too.

Thank you for the kind words! I really appreciate the opportunity to contribute here. Excited to see the project moving forward again, and glad the types will be added to package.json 🙌

umidjon-2231 avatar Sep 01 '25 08:09 umidjon-2231