Skip to content

Conversation

umidjon-2231
Copy link
Contributor

@umidjon-2231 umidjon-2231 commented Mar 5, 2025

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!

@R1D3R175
Copy link

R1D3R175 commented Apr 7, 2025

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.

@umidjon-2231
Copy link
Contributor Author

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

@j-d-carmichael
Copy link

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

Copy link
Owner

@eKoopmans eKoopmans left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@eKoopmans eKoopmans merged commit 57bcf0a into eKoopmans:main Aug 31, 2025
@ekoopmans-github-release-tokens
Copy link
Contributor

🎉 This PR is included in version 0.11.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

@umidjon-2231
Copy link
Contributor Author

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 🙌

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 this pull request may close these issues.

4 participants