Generic method in Typescript

Viewed 12246

I have an abstract class which has an abstract generic method, which looks something like this

protected abstract getData<T>(): T[];

Then I have a class which extends this abstract class. Since getData is abstract I have to make implementation of it in the child class.

And it looks something like this

protected getDataList<T>(): T[] {
    return this.databaseService().getSomethingList();
}

getSomethingList()returns Something[]

I get following error though

Type 'Something[]' is not assignable to type 'T[]'.

I tried few things to resolve this error, but seem to have to make the child implementation generic as well for Typescript to be happy. The above implementation was fine until I upgrade Typescript from 2.2.1 to 2.4.1.

So I'm wondering how I can make my code compliant again with Typescript 2.4.1?

3 Answers
Related