0

How fix this error? enter image description here

I want to wrap each object field from the getters argument in a ComputedRef wrapper

import { computed, ComputedRef } from "vue";

function useComputedGroup<T extends Record<string, any>>(getters: T) {
    type NewData = {
      [K in keyof T]: ComputedRef<T[K]>;
    };

    const newData: Partial<NewData> = {};
    Object.keys(getters).forEach(
      (key) => (newData[key] = computed(() => getters[key]))
    );

    return newData;
}
Den Vol
  • 19
  • 3
  • There are [a couple of hundred](/search?q=%5Btypescript%5D+string+cannot+be+used+to+index+type) questions about this, at least dozens of them with answers. Please search thoroughly before posting. More about searching [here](/help/searching). – T.J. Crowder May 28 '22 at 14:01
  • Does this answer your question? [string cannot be used to index type 'T'](https://stackoverflow.com/questions/63893394/string-cannot-be-used-to-index-type-t) – codewario May 29 '22 at 18:26

1 Answers1

-1

At random, I was able to find a solution, but I can’t explain it to myself

function useComputedGroup<T extends Record<string, any>>(getters: T) {
    type NewData = {
      [K in keyof T]: ComputedRef<T[K]>;
    };

    const newData: Partial<NewData> = {};
    const keys: Array<keyof T> = Object.keys(getters); // !!!

    keys.forEach((key) => (newData[key] = computed(() => getters[key])));

    return newData;
  }
Den Vol
  • 19
  • 3