Adding Computed Property Destructuring to Vue #729
Closed
bosens-China
started this conversation in
RFC Discussions
Replies: 2 comments 2 replies
-
This would be impossible to type with TypeScript. You would need const x = computed(() => ({ a: 1, b: 2 }));
// { value: { a: number, b: number } }
const { a, b } = computed(() => ({ a: 1, b: 2 }));
// { a: { value: number }, b: { value: number } } |
Beta Was this translation helpful? Give feedback.
1 reply
-
This has a few deal breakers: it's a huge breaking change, it an inconsistent behavior, and can be implemented in user land (Nested support can be achieved with Proxies). It could maybe also be a compile time but would require a different kind of |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Background
In Vue 3,
computed
properties are commonly used as reactive tools to derive new data from existing state. However, when acomputed
property returns an object, directly destructuring it results in the loss of reactivity for the destructured properties. To maintain reactivity, developers currently have to manually create newcomputed
properties for each field, as shown below:This approach becomes verbose and cumbersome, especially when dealing with objects with many properties or deeply nested structures. It reduces code readability and development efficiency.
Significance and Usage
Significance:
computed
properties like regular objects, while maintaining reactivity, which enhances code clarity.Usage:
With the proposed improvement, computed properties can be destructured directly. Here's an example:
Support for Nested Destructuring:
If a computed property returns a nested object, it should also allow nested destructuring, with all destructured properties remaining reactive:
Completeness and Compatibility:
By introducing this feature, Vue’s
computed
properties would become even more developer-friendly, reducing redundancy and maintenance overhead while enhancing productivity and code readability.Beta Was this translation helpful? Give feedback.
All reactions