T [keyof T]不能用于索引{}

时间:2020-05-12 15:46:53

标签: javascript typescript

我试图通过Most efficient method to groupby on an array of objects中的alogrithm实现高效的分组,并尝试放入类型。但是我得到了错误:

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;
        }, {});
    };

3 个答案:

答案 0 :(得分:0)

rv: any rv将为任何

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[]>;

同步组返回一个对象,并且对象不能具有除string | number之外的任何其他元素作为键。否则,您可以遵循更多通用的解决方案。

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;
    }, {});
  }
}

答案 1 :(得分:0)

如果您不想使用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是一个对象,其中传递的键的类型可以用作键(对于rv)。

对于reducer-它以一个空对象开头-我们需要说结果将是一个对象,其中键的值是xs {[LK2 in T[K]]?: Array<T>}中的实体数组

答案 2 :(得分:0)

不如原始的短。但是,它不使用“ 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()) {
}