【问题标题】:Generic type requires 1 type argument(s) error泛型类型需要 1 个类型参数错误
【发布时间】:2021-10-24 09:12:55
【问题描述】:

我有一个抽象类,例如:

export abstract class CsvFileReader<T> {
  data: T[] = []

  constructor(public file: string) {}

  abstract mapRow(row: string[]): T

  read() {
    this.data = this.file
      .split('\n')
      .map((row: string): string[] => {
        return row.split(',')
      })
      .map(this.mapRow)
  }
}

以及在抽象类之上扩展的类:

type matchData = [Date, string, string, number, number, MatchResualts, string]

export class MatchReader extends CsvFileReader<matchData> {
  mapRow(row: string[]): matchData {
    return [
      dateStringToDate(row[0]),
      row[1],
      row[2],
      +row[3],
      +row[4],
      row[5] as MatchResualts,
      row[6],
    ]
  }
}

我像这样创建阅读器:

const reader = new MatchReader(Matches)

但我收到此错误:通用类型“CsvFileReader”需要 1 个类型参数。

有什么办法吗?

【问题讨论】:

  • 我不确定这是否有帮助,但我将您的代码(仅没有 exports 和未知的 dateStringToDateMatchResualts 项目,以及虚拟文件内容)放入TypeScript Playground,然后它可以毫无问题地编译和运行。这是我使用的代码:pastebin.com/5xFBfedZ

标签: javascript typescript


【解决方案1】:

对不起,我的第一个答案是错误的。

你需要做这样的事情:const reader: CsvFileReader&lt;matchData&gt; = new MatchReader(Matches)

这行得通吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多