kaplay icon indicating copy to clipboard operation
kaplay copied to clipboard

feat: return obj with components on `.use()` and `.unuse()`

Open lajbel opened this issue 1 year ago • 4 comments

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.

lajbel avatar Nov 14 '24 23:11 lajbel

why not planned???

amyspark-ng avatar Apr 12 '25 02:04 amyspark-ng

oops reopened on accident im so sorry

amyspark-ng avatar Apr 12 '25 02:04 amyspark-ng

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

lajbel avatar Apr 12 '25 13:04 lajbel

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.

mflerackers avatar Apr 12 '25 13:04 mflerackers