-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Request for MapWritableState nested object access. #634
Comments
I'm personally not a fan of this syntax unless it manages to:
Another alternative that could work in TS but is way more verbose and IMO not intuitive would be supplying a function and a key mapWritableState(useStore, { city: [state => state.adress, 'city'] })
mapWritableState(useStore, { city: { getter: state => state.adress, key: 'city'] }}) |
How about ? mapWritableState(useStore, ['form', 'address', 'city']) |
that syntax would be confusing with the existing one |
This should be doable with this TS type: export type ObjectPaths<
O extends Record<string, any>,
Path extends string = ``
> = keyof O extends string
?
| `${Path}${keyof O}`
| _ObjectValues<{
[K in keyof O as O[K] extends Record<string, any>
? K extends string
? K
: never
: never]: ObjectPaths<O[K], `${Path}${K}.`>
}>
: never
type _ObjectValues<T> = T extends Record<string, infer R>
? Extract<R, string>
: never
const nestedObj = {
a: 'hey',
b: { a: 'hey', b: 'hello', c: { d: 'hey', e: 'f' } },
nested: { a: { b: { c: { d: 'e' } } } },
}
function acceptKeys<T>(o: T, keys: ObjectPaths<T>[]) {}
acceptKeys(nestedObj, ['a', 'b', 'b.a', 'no', '']) Feel free to pick it up from here if you want. It's worth noting that right now we accept property keys with a |
An alternative might be to create partitioned 'child' stores from a parent store. Then the addressing of any particular entry would be 'top level' again. This could even potentially be done within a setup function (and have the dynamic binding change as the values bound from setup changed reactively). Syntax for OP's case would end up like...
...which could potentially be accelerated by a path-oriented syntax...
It also has the beneficial side-effect of being able to author form items that do not know whether the item they are bound to is the WHOLE store, or just a sub-part, since they just address their binding relative to the partitioned store. This potentially makes it easier to refactor stores. |
I also think this would be very helpful. I'm not using typescript or keys with In the past (Vue2) we used vuex-pathify and they had some nice helper functions computed: {
id: get(store, 'profile.id'),
name: sync(store, 'profile.name'),
}, In regard to mapWritableState(useStore, {
foo: 'a[0].b.c'
}, true) // nested = true Or a helper function that generates a function: mapWritableState(useStore, {
foo: nested('a[0].b.c')
}) I'm don't use TS autocomplete etc, so not sure if those ideas would translate. |
We have lots of code where we pass the full path to the state and use only the last 'key' as the local name. Would love to see this working in Pinia, because it is the only thing holding us back of migrating from Vuex -> Pinia. |
Is there any update to this? We'd also love to be able to do this or just pass a function like with mapState(). :) |
+1, this would be great to have |
What problem is this solving
Modifying deeply nested properties in the state.
Proposed solution
The text was updated successfully, but these errors were encountered: