jonobr1 / two.js

A renderer agnostic two-dimensional drawing api for the web.
https://two.js.org
MIT License
8.29k stars 454 forks source link

[Bug] Typescript complains about incompatibility when typechecking `Two.Element` #628

Closed cyndra0 closed 2 years ago

cyndra0 commented 2 years ago

Describe the bug By definition, any kind of objects are extended from Two.Element, so it should be ok to give a Text to where asking for an Element. But this call fails in v0.8.6:

const two = new Two()
two.add(new Two.Text()) // Error: Argument of type 'Text' is not assignable to parameter of type 'Element'

Environment (please select one):

Additional context When I peek the type definition I found that Element refers to the Element in lib.dom.d.ts instead of Two.Element. It turns out to be a name collision between two.js and dom. https://github.com/jonobr1/two.js/blob/7db7ba6be546f305cd63431941266c7b8a2e42c0/types.d.ts#L4103

jonobr1 commented 2 years ago

Thanks for PRing the solution!

endquote commented 2 years ago

I ran into this conflict and was waiting for this change to land before updating my two.js version from 0.8.5. In 0.8.8 I think I'm seeing the same or a similar issue.

Here I'm adding a Rectangle to a Group, the error seems to make it seem like a DOM element is expected.

Screen Shot 2022-05-31 at 6 57 26 PM

jonobr1 commented 2 years ago

Ah thanks, good eyes @endquote. This commit should fix that: https://github.com/jonobr1/two.js/commit/ccb29aa9a9b2dd798b1fd537ef89b496be522f6a

I'll do a soft update on npm to 0.8.9: https://www.npmjs.com/package/two.js/v/0.8.9

endquote commented 2 years ago

Yes, looks good now! 🎉