2

I have abstract class:

abstract class AScore<T> {
  constructor(
    protected data: T) {}
}

I implement this class like:

class GetActivitiesPupil implements AScore<number> {}

Compiler says it is wrong implementation of class

Ben Smith
  • 19,589
  • 6
  • 65
  • 93
POV
  • 11,293
  • 34
  • 107
  • 201

1 Answers1

3

You want to extend the abstract class to create a concrete instance i.e.

abstract class AScore<T> {
  constructor(protected data: T) {}
}

class GetActivitiesPupil extends AScore<number> {
  data: number;

  constructor(data: number) {
   super(data)}
  }
}

const test = new GetActivitiesPupil(123);
console.log(test.data) // Outputs 123

You can see that this code has no errors here.

Ben Smith
  • 19,589
  • 6
  • 65
  • 93
  • Why you added this: `data: number;`? – POV Apr 16 '19 at 12:41
  • Why I ca not pass T in `GetActivitiesPupil`? – POV Apr 16 '19 at 12:42
  • 1
    You are passing T in GetActivitiesPupil; in this case T is number. I'm including "data: number" as a concrete instance would store the data defined in the abstract base class. This code would work without "data: number" but in practice it would a useless class. – Ben Smith Apr 16 '19 at 12:44