小编t-M*_*URO的帖子

如何一般地键入返回对象数组的值数组的函数?

我正在尝试键入一个函数,该函数返回一个通用对象数组的值数组(用于获取表的所有列值)。

class DataSource<T> {
  
  constructor(private data: T[]) {}

  public getColumnValues(column: keyof T): any[] { // this is the thing I don't know how to type!
    return data.map(entry => entry[key]);
  }
}
Run Code Online (Sandbox Code Playgroud)

目标是拥有一个显示正确返回值的通用签名。例如:getColumnValues('someDate') 应该是 Date[]getColumnValues('someString')作为 string[]

我尝试搜索这个,但我可能无法正确表达我的问题......

typescript typescript-generics typescript-typings

3
推荐指数
1
解决办法
69
查看次数