Skip to content

Add support of typescript #738

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

umidjon-2231
Copy link

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

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

Successfully merging this pull request may close these issues.

2 participants