Add support of typescript
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, andHtml2PdfStatic. - Ensured all documented methods and parameters are typed correctly.
- Included JSDoc comments for better documentation.
Motivation & Benefits:
- Enables TypeScript developers to use
html2pdf.jswith type checking. - Enhances code editor autocompletion and documentation.
- Reduces potential runtime errors by enforcing correct API usage.
How to Test:
- Import the library in a TypeScript project:
import html2pdf from "html2pdf.js"; - Use the library with the new type definitions:
const element = document.getElementById("element-to-print"); html2pdf().from(element).set({ filename: "test.pdf" }).save(); - Verify that TypeScript recognizes the method signatures and shows relevant hints in an IDE.
Looking forward to feedback and suggestions!
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 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.
@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",
...
: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 🙌