类型为T[K]的键映射回T本身的Typescript通用映射

类型为T[K]的键映射回T本身的Typescript通用映射,typescript,typescript-generics,Typescript,Typescript Generics,我有以下代码: type PartialRecord<K extends keyof any, T> = { [P in K]?: T; }; export class CustomMaps { public static createInnerMap<T, K extends keyof any>(array: T[], customKey: keyof T): PartialRecord<K, T> { const innerMap: P

我有以下代码:

type PartialRecord<K extends keyof any, T> = {
  [P in K]?: T;
};

export class CustomMaps {
  public static createInnerMap<T, K extends keyof any>(array: T[], customKey: keyof T): PartialRecord<K, T> {
    const innerMap: PartialRecord<K, T> = {};
    for (let i = 0; i < array.length; ++i) {
      innerMap[array[i][customKey] as K] = array[i];
    }
    return innerMap;
  }
}
type PartialRecord={
[P在K]?:T;
};
导出类自定义地图{
公共静态createInnerMap(数组:T[],customKey:keyof T):PartialRecord{
const innerMap:PartialRecord={};
for(设i=0;i
它给出了一个错误:将类型“T[keyof T]”转换为类型“K”可能是一个错误,因为两种类型都没有足够的重叠。如果这是有意的,请先将表达式转换为“未知”

它应该做的是:

  class Car {
    private color: string;
    private wheelcount: number;
    constructor(color: string, wheelcount: number) {
      this.color = color;
      this.wheelcount = wheelcount;
    }
  }
  const redCar = new Car('red', 4);
  const blueCar = new Car('blue', 8);
  const yellowCar = new Car('yellow', 6);
  const colorMap = CustomMaps.createInnerMap<Car, string>([redCar, blueCar, yellowCar], 'color');
  colorMap['red'] // should return redCar instance

  const wheelMap = CustomMaps.createInnerMap<Car, number>([redCar, blueCar, yellowCar], 'wheelcount');
  wheelMap[8] // should return blueCar instance
等级车{
私有颜色:字符串;
私家车轮数:车号;
构造函数(颜色:字符串,轮数:数字){
这个颜色=颜色;
this.wheelcount=车轮计数;
}
}
const redCar=新车('red',4);
const blueCar=新车(“蓝色”,8);
const yellowCar=新车(“黄色”,6);
const colorMap=CustomMaps.createInnerMap([redCar,blueCar,yellowCar],'color');
colorMap['red']//应返回redCar实例
const wheelMap=CustomMaps.createInnerMap([redCar,blueCar,yellowCar],'wheelcount');
wheelMap[8]//应该返回blueCar实例

如果不将innerMap[array[i][customKey]]强制转换为unknown,我如何确保array[i][customKey]将返回一个可用于索引的类型(特别是i类型替代K)?

看来我已经找到了解决方案,而不必使用强制转换“与K一样未知”的脏修复程序

我制作了以下自定义类型:

type TRecordKey = string | number | symbol;
type TRecurringRecord<K extends T[keyof T] & TRecordKey, T> = {
  [P in K]?: T
}
type TRecordKey=string | number |符号;
类型TRecurringRecord={
[P in K]?:T
}
然后我调整了上面的代码如下:

  public static recurringMap<K extends T[keyof T] & TRecordKey, T>(
    array: T[],
    customKey: keyof T
  ): TRecurringRecord<K, T> {
    const recurringMap: TRecurringRecord<K, T> = {};
    for (let i = 0; i < array.length; ++i) {
      recurringMap[array[i][customKey] as K] = array[i];
    }
    return recurringMap;
  }
publicstaticrecurringmap(
数组:T[],
customKey:keyof T
):TRecurringRecord{
const recurringMap:TRecurringRecord={};
for(设i=0;i

现在我们有了一个通用函数,它可以创建一个对象的映射,其中映射的键是对象本身的属性。

看来我已经找到了解决方案,而不必使用强制转换“像K一样未知”的肮脏修复

我制作了以下自定义类型:

type TRecordKey = string | number | symbol;
type TRecurringRecord<K extends T[keyof T] & TRecordKey, T> = {
  [P in K]?: T
}
type TRecordKey=string | number |符号;
类型TRecurringRecord={
[P in K]?:T
}
然后我调整了上面的代码如下:

  public static recurringMap<K extends T[keyof T] & TRecordKey, T>(
    array: T[],
    customKey: keyof T
  ): TRecurringRecord<K, T> {
    const recurringMap: TRecurringRecord<K, T> = {};
    for (let i = 0; i < array.length; ++i) {
      recurringMap[array[i][customKey] as K] = array[i];
    }
    return recurringMap;
  }
publicstaticrecurringmap(
数组:T[],
customKey:keyof T
):TRecurringRecord{
const recurringMap:TRecurringRecord={};
for(设i=0;i

现在我们有了一个通用函数,可以创建对象的映射,其中映射的键是对象本身的属性。

这很难理解。您能否发布一些
createInnerMap
的使用示例?也许可以进一步解释一下这段代码的目的是什么?@Alex Wayne您好,我采纳了您的建议,用更清晰的代码和示例对其进行了修改。这很难理解。您能否发布一些
createInnerMap
的使用示例?也许可以进一步解释一下这段代码的目的是什么?@Alex Wayne您好,我采纳了您的建议,用更清晰的代码和示例对其进行了修改。