![](/img/trans.png)
[英]Typescript 'keyof InstanceType<T>' cannot be used to index type error
[英]T[keyof T] cannot be used to index {}
我正在尝试通过算法从最有效的方法到 groupby 在对象数组上实现一个有效的分组,并尝试放置类型。 但我得到了错误:
T[keyof T] cannot be used to index {}
这是我的尝试
static groupBy<T>(xs:T[], key: keyof T) {
return xs.reduce((rv, x)=> {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
};
rv: any
rv 都将是any 。
class A {
static groupBy<T>(xs: T[], key: keyof T) {
return xs.reduce((rv: any, x) => {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
}
}
如何使用它:
interface Person {
name: string;
age: number;
salary: number;
}
const data: Person[] = [
{name:"deepak", age: 30, salary: 2000},
{name:"deepak1", age: 32, salary: 1000},
{name:"deepak", age: 29, salary: 3000}
]
class A {
static groupBy<T>(xs: T[], key: keyof T) {
return xs.reduce((rv: any, x) => {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
}
}
console.log(A.groupBy(data, "name"))
Lodash的定义:
groupBy(
predicate?: Lodash.ListIterator<T, boolean> | Lodash.DictionaryIterator<T, boolean> | string,
thisArg?: any,
): Lodash.Dictionary<T[]>;
groupBy<R extends {}>(predicate?: R): Lodash.Dictionary<T[]>;
同步组返回 object 和 object 不能将任何其他元素作为键,而不是字符串|数字。 否则,您可以遵循更多通用解决方案。
interface Any<T> {
[key: string]: T[];
}
interface SMap<T> {
[key: string]: T;
}
class A {
static groupBy<T extends SMap<string>>(xs: T[], key: keyof T) {
return xs.reduce((rv: Any<T>, x) => {
if (!rv[x[key]]) {
rv[x[key]] = [];
}
rv[x[key]].push(x);
return rv;
}, {});
}
}
如果您不想使用any
,则需要正确定义类型以告诉 reducer 可以组合数据。
function groupBy<T extends {[LK in K]: keyof any}, K extends keyof T>(xs:T[], key: K) {
return xs.reduce<{[LK2 in T[K]]?: Array<T>}>((rv, x) => {
(rv[x[key]] = rv[x[key]] || [])?.push(x);
return rv;
}, {});
};
const result = groupBy([{ test: 'key' }], 'test');
result.key?.length; // 1
T 是 object,其中传递的密钥类型是可以用作密钥的东西(对于 rv)。
对于减速器 - 它以空 object 开头 - 我们需要说结果将是 object ,其中键的值是来自 xs {[LK2 in T[K]]?: Array<T>}
的实体数组
没有原版那么短。 但是,它不使用“any”或“as”强制转换。 它还支持组键的任何数据类型,因此是未知的。
export function groupBy<T>(xs: T[], key: keyof T): Map<unknown, T[]> {
return xs.reduce((rv: Map<unknown, T[]>, entity: T) => {
const value = entity[key];
if (rv.has(value)) {
rv.get(value)?.push(entity)
} else {
rv.set(value, [entity]);
}
return rv;
}, new Map());
};
用法示例:
const badgesByTypes = groupBy(currentState.badges, 'type');
for (const [key, values] of badgesByTypes.entries()) {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.