Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isReadonly returns false for computed value (without a set function) nested in a reactive object #12138

Closed
cloydlau opened this issue Oct 10, 2024 · 5 comments

Comments

@cloydlau
Copy link
Contributor

cloydlau commented Oct 10, 2024

Vue version

3.5.11

Link to minimal reproduction

https://play.vuejs.org/#eNqNk19L7DAQxb/KkJd2oXRZvPdl0YV7ZR9UUFEfA1LS6RrtJmGSrruUfncnra2Cf9i3cM6vkzMzTSv+OZfvGhRLceoVaRfAY2jcShq9dZYCtKDs1jUBywy0v8OitKY+ZEBYqKB32J96DTqoyG4h4XqJNNIoa3wAgrMJSdtuNuqK9bF0ms7gbAWLybRstkAZU91UKIrjtalldnBsjXltN+lHupRmM5jPIVCDPzI2P4ai4zB11IXHUHzhO1YVtWdOGj5zCm6+MSVW2mAZ7ZurdycO8osTc/+gf+EfDg7XRJaWkPD2E7AGHNn9YclRC8czDw1FNibS/gkqSxFwSOEAieJln86Hv2clMhE8t1bpTf7sreEfq5UGQIq4a10j3biguXUpltA70Svq2r5e9locTjbq6gnVyzf6s99HTYpbQo+0QykmLxS0wTDY6/tr3PN5Mre2bGqmfzHvkNfSxIwD9p9nxbE/cX3ai/55aLN58Ot9QOPHpobtAnQ9LwW/hvNfWv+Ie5L/6b+TpuMpPu6QYk0e4En+N18sRPcGZ8o72Q==

Steps to reproduce

  1. Open the reproduction link
  2. Check the Console

What is expected?

  1. Computed value (without a set function) is readonly, so maybe those nested in a reactive object should also be readonly, right?
  2. The PR below shows that mutating a readonly ref nested in a reactive object is not allowed, but it's not clear for this case
    fix(reactivity): mutating a readonly ref nested in a reactive object should fail. (close #5042) #5048

What is actually happening?

  1. isReadonly(ro.c) returns false
  2. ro.c = undefined throws TypeError: 'set' on proxy: trap returned falsish for property 'c'

System Info

No response

Any additional comments?

No response

@cloydlau cloydlau changed the title isReadonly returns false for computed value nested in a reactive object isReadonly returns false for computed value (without a set function) nested in a reactive object Oct 10, 2024
@jh-leong
Copy link
Member

This behavior is expected, see https://vuejs.org/api/reactivity-core.html#reactive:

A reactive object also deeply unwraps any properties that are refs while maintaining reactivity.

In your case, ro.c is a number not a computed, so isReadonly(ro.c) returns false

@cloydlau
Copy link
Contributor Author

OK with the first point.

ro.c is a number not a computed

Then why ro.c = undefined throws TypeError: 'set' on proxy: trap returned falsish for property 'c'?

@jh-leong
Copy link
Member

This is also related to Ref unwrapping.

When you try to update ro.c, you're effectively trying to update the computed value, which is not allowed, leading to the error.

@cloydlau
Copy link
Contributor Author

I think it's kind of contradictory and confusing to have a variable that isReadonly returns false but doesn't allow it to be mutated. If you think this is in line with expectations, I'd personally recommend at least stating it in the documentation.

@jh-leong
Copy link
Member

You're right, the current error message doesn't make it clear that this behavior is due to Ref unwrapping. I've submitted a PR try to improve the warning in #12141:

Before:

image

After:

image

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants