maxrects-packer icon indicating copy to clipboard operation
maxrects-packer copied to clipboard

addArray examples violate typescript types

Open JackCA opened this issue 2 years ago • 2 comments

It appears that the addArray function only accepts types extended from IRectangle. This appears to be an issue because IRectangle includes values that should not be set during addition of rectangles (such as x and y).

e.g.

// packer: MaxRectsPacker<IRectangle>
packer.addArray([{width: 1, height: 1}]) // Type '{ width: number; height: number; }' is missing the following properties from type 'IRectangle': x, y ts(2739)

I think the generic needs to be more narrowed to something like

interface IRectangleInput {
  width: number
  height: number
  [propName: string]: any
}

perhaps porting the tests to typescript would catch this?

Thanks for making this lib either way 🙏

JackCA avatar Oct 01 '22 03:10 JackCA

You are right, I'll look into it. Thx As intended, addArray should accept any object with width & height

soimy avatar Nov 07 '22 04:11 soimy

My fix was to use a Rectangle[]. The Rectangle constructor sets x and y to 0 by default and is happy to accept just width and height.

SkipHendriks avatar Jan 11 '23 22:01 SkipHendriks