Consider the following code, where I’m trying to override and also specify the parameter passed to a generic class method:
class MyBaseClass {
getData<T extends MyBaseClass>(classes: {[key: string]: T}){}
}
class FirstChild extends MyBaseClass {
private data: number[];
constructor(){ data = [//get data]}
}
// e.g. 1
class SecondChild extends MyBaseClass {
private data: number[];
getData<FirstChild>(classes: {category: FirstChild}){} // error
}
now, this gives an error and for it to be error free, I’m gonna have to change SecondChild
to:
class SecondChild extends MyBaseClass {
getData(classes: {[key: string]: FirstChild}){} // error
}
I’m wondering if something similar to my first example (e.g.1
) is possible. What I want is to generalize the base class but, specify the elements that I pass to the child class.
For example, if it were possible I could get specific keys for extended entities while not worrying about them in parent. E.g. 2:
class ThirdChild extends MyBaseClass {
getData(classes: {category1: FirstChild}, category2: SecondChild){
const categoryAData = category1.getData(),
const categoryBData = category2.getData(),
}
}
Is there a way to somehow achieve this?