简体   繁体   中英

Generic type requires 1 type argument(s) error

I have an abstract class like:

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)
  }
}

and a class that extends above abstract class:

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],
    ]
  }
}

I create reader just like this:

const reader = new MatchReader(Matches)

but i get this error: Generic type 'CsvFileReader' requires 1 type argument(s).

any solution?

Sorry my first answer was wrong.

you need to do something like this: const reader: CsvFileReader<matchData> = new MatchReader(Matches)

Does this work?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM