2

I am trying to use the Composition API of vue 3, and struggle to handle reactive typescript objects. As I understand from the documentation, when you make an object reactive, vue wraps it and all nested objects into a Proxy and converts all Refs to Proxies as well.

The problem is that when you have a chain of nested method calls, only the first of them is invoked on Proxy wrapper (see the attached diagram for an example), and all of the remaining ones are invoked on standard typescript objects.

This inconsistency makes developing an app really hard - I have to basically support two versions of each method dealing with references - one for calling on Proxy and one for calling on a normal instance + check object types in runtime. There is not a word about such problem in the documentation. Am I right in my attempts to solve this problem, or should I throw an exception when some methods are not invoked as expected, or there is a better way of solving this issue?

example: dependency diagram

I've tried to implement a typescript object, the methods of which can be called through both - reactive and non-reactive contexts.

I expected the object to handle Refs consistently regardless of whether it is called from reactive or non-reactive context.

In reality, when context is reactive, current object is actually a Proxy, and all properties typed as Refs are also Proxies, so I don't have to call value property on them to access their members. However, when the context is not reactive, all properties typed as Refs remain Refs and I have to go through their value property before accessing their nested members.

zeionara
  • 21
  • 2
  • "I expected the object to handle Refs consistently" - the expectation is wrong, this is basic and documented behaviour, it's generally preferable with regular composition api usage https://vuejs.org/guide/essentials/reactivity-fundamentals.html#ref-unwrapping-in-reactive-objects . It's the use of classes that is primary problem here, you'll have to jump through hoops to use them efficiently, some points are here https://stackoverflow.com/a/76247596/3731501 – Estus Flask May 16 '23 at 15:40
  • As for the specific problem with corge ref, it can be solved with # native private field and a pair of class getter setters if it needs to be accessed publicly, so it would be ignored by vue class reactivity. But it's a point solution to a bigger problem – Estus Flask May 16 '23 at 17:24

0 Answers0