flauwekeul / honeycomb

Create hex grids easily, in node or the browser.
https://abbekeultjes.nl/honeycomb
MIT License
639 stars 59 forks source link

Grid constructor typing with custom Hex class #106

Closed namiwang closed 1 year ago

namiwang commented 1 year ago

new Grid() always has a type of Grid<Hex>, even with a custom Hex class.

A workaround is new Grid(CustomHex, spiralTraverser) as unknown as Grid<CustomHex>

flauwekeul commented 1 year ago

Thanks for opening the issue.

I see it's broken This only happens when just calling defineHex() and assigning the result to a variable (not using class), right?

const CustomHex = defineHex()
const grid = new Grid(CustomHex)
//    ^ Grid<Hex>

Or do you see more scenarios where you get Grid<Hex> and expect something else?

This has the expected type:

class CustomHex extends defineHex() {}
const grid = new Grid(CustomHex)
//    ^ Grid<CustomHex>
flauwekeul commented 1 year ago

With this code:

const CustomHex = defineHex()
const grid = new Grid(CustomHex)
//    ^ Grid<Hex>

I don't see how it's possible to make grid have the type Grid<CustomHex> (except for what you already mentioned: as unknown as Grid<CustomHex>).

So, unless there are other scenarios where you expect Grid<CustomHex> but get Grid<Hex> as a type, this seems a non-issue.

namiwang commented 1 year ago

I'm indeed using a custom class like this

export default class CDHex extends Hex { ... }
const spiralTraverser = spiral( ... )
const grid = new Grid(CustomHex, spiralTraverser)
      ^ Grid<Hex> by default

And I just found that the correct way to infer the type is to add typings to spiral

export default class CustomHex extends Hex { ... }
const spiralTraverser = spiral<CustomHex>( ... )
const grid = new Grid<CustomHex>(CustomHex, spiralTraverser)
      ^ Grid<CustomHex>

So yeah, I think there's no issue here. Thanks for the details.