TS2536:类型 'string' 不能用于索引类型 'Partial<NewData>'
TS2536: Type 'string' cannot be used to index type 'Partial<NewData>'
如何解决这个错误?
我想将来自 getters 参数的每个对象字段包装在 ComputedRef 包装器中
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;
}
我随机找到了一个解决方案,但我无法向自己解释
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;
}
如何解决这个错误?
我想将来自 getters 参数的每个对象字段包装在 ComputedRef 包装器中
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;
}
我随机找到了一个解决方案,但我无法向自己解释
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;
}