Skip to content
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

Wrong TypeScript type in jsPDF.table #3086

Closed
jabaa opened this issue Feb 4, 2021 · 4 comments
Closed

Wrong TypeScript type in jsPDF.table #3086

jabaa opened this issue Feb 4, 2021 · 4 comments

Comments

@jabaa
Copy link
Contributor

jabaa commented Feb 4, 2021

The declaration for jsPDF.table in types/index.d.ts is

table(
  x: number,
  y: number,
  data: any,
  headers: string[],
  config: any
): jsPDF;

but in the spec tests in test/specs/cell.spec.js the headers are passed as

headers: {
  name: string;
  prompt: string;
  width: number;
  align: "center" | "left" | "right";
  padding: number
}[]

Changing the headers to a list of strings

var header = [
  "coin",
  "game_group",
  "game_name",
  "game_version",
  "machine",
  "vlt"
]

causes the tests to fail. It seems like it's not possible to create a table in TypeScript without ignoring the declaration.

@HackbrettXXX
Copy link
Collaborator

The code suggests that headers may either be an array of strings or and array of {name, prompt, align} objects. If the array of strings isn't working, this is probably a bug. Nonetheless, we should extend the typings to also accept objects.

I think we can also improve the type of the data argument to something like Record<string,string|number|boolen>[].

Could you create a pull request? That would be awesome!

@jabaa
Copy link
Contributor Author

jabaa commented Feb 8, 2021

The bug is probably here. If typeof headers[0] === "object" is true columnWidths is set but in case typeof headers[0] === "string" is true columnWidths isn't set and this causes an error in rect. I don't know how to set columnWidths for an array of strings.

@HackbrettXXX
Copy link
Collaborator

I think strings will only work if autoSize is true. We might change the condition here to something like:

if (autoSize || (Array.isArray(headers) && typeof headers[0] === "string")) {

HackbrettXXX pushed a commit that referenced this issue Feb 10, 2021
* also implicitly calculate cell widths if headers are passed as string array
sthagen added a commit to sthagen/parallax-jsPDF that referenced this issue Feb 10, 2021
Fix: Wrong TypeScript type in jsPDF.table (parallax#3086) (parallax#3087)
This was referenced Mar 10, 2021
This was referenced Mar 12, 2021
@HackbrettXXX
Copy link
Collaborator

Fixed.

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

No branches or pull requests

2 participants