Skip to content

Instantly share code, notes, and snippets.

@cefn
Created April 26, 2022 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cefn/c4b3ab064c55dd8c05b73092036bb1e9 to your computer and use it in GitHub Desktop.
Save cefn/c4b3ab064c55dd8c05b73092036bb1e9 to your computer and use it in GitHub Desktop.
Possible follow implementation to map named children to named VM properties
import { computed, ComputedRef } from 'vue';
export function followDescendantsByName<
ReactiveParent,
ChildKeys extends ReadonlyArray<keyof ReactiveParent>,
>(parentSelector: () => ReactiveParent, childKeys: ChildKeys) {
return Object.fromEntries(
childKeys.map((childKey) => {
return [childKey, computed(() => parentSelector()[childKey])];
}),
) as {
[childKey in ChildKeys[number]]: ComputedRef<ReactiveParent[childKey]>;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment