To establish an abstract class in typescript, we can name it Entity
, which contains an abstract method called constructorProps()
that returns the array of properties required to build the derived class.
For instance, if Foo extends Entity
and does not have a constructor, then the return type of constructorProps()
should be an empty tuple:
class Entity<Child extends Entity<any>> {
abstract constructorProps(): // What is the correct syntax here? ???
}
class Foo extends Entity<Foo> {
override constructorProps() {
return []
}
}
However, suppose Bar extends Entity
and has a constructor accepting one argument. In that case, constructorProps()
should consistently return a [string]
:
class Bar extends Entity<Bar> {
name: string;
constructor(name: string) {
this.name = name;
}
override constructorProps() {
return [this.name];
}
}
The initial clue lies in passing the extended class as a type parameter so that the abstract class can refer to it. Despite this insight, achieving the intended outcome remains uncertain.
- Is there a method to acquire the constructor's type (with arguments) from an instance type such as
ConstructorOf<Bar>
? - Am I taking the right approach here, or are there alternative approaches available?