feat: return obj with components on `.use()` and `.unuse()`
Discussed in https://github.com/kaplayjs/kaplay/discussions/510
Originally posted by white16819 November 13, 2024
I'm newer to kaplay and ts.
How to add AreaComp's feature such as onCollide to a existed GameObj like below:
const text = k.add([k.text("Test")])
I try to use function like this below:
text.use(k.area())
Expect the 'text' GameObj's type change into GameObj<TextComp | AreaComp>,
but the 'text' Gameobj's type still be GameObj<TextComp>.
So if I invoke the 'onCollide' function in AreaComp, the ts check will raise the error:
Property 'onCollide' does not exist on type 'GameObj<TextComp>'
Waiting for your suggestions.
why not planned???
oops reopened on accident im so sorry
We can't do it that easy. Our best bet is return the new type on obj.use(), so you can use the updated object type. But it can't work dynamically
I think returning a fresh type on use is acceptable, it's impossible in any typed language to mutate the type of an existing reference.