Is it possible to develop an abstract constructor for an abstract class in TypeScript?
Let's consider the following abstract class as an example:
export abstract class Foo<T> extends Bar<T> {
constructor(someParam: any) {
super();
this.someObj = someParam;
}
}
This abstract class is then extended by the following class:
export class FooImpl extends Foo<SomeType> {
}
If the constructor is not overridden, the default parameterless constructor will be used in the FooImpl
class, resulting in an incorrect state within the class.
How can we define an abstract constructor or compel consumers of the abstract class to override the constructor when consuming Foo<T>
?
UPDATE
The specific code I have encountered is shown below and seems to be related to vscode:
export abstract class FireStoreDataSource<T> extends DataSource<T> {
constructor(store: () => Observable<T[]>) {
super();
}
}
I am extending this class as follows:
export class AssessmentCenterDataSource extends FireStoreDataSource<AssessmentCenter> {
}
Then, creating an instance in an Angular component's ngOnInit
:
ngOnInit() {
this.dataSource = new AssessmentCenterDataSource(() => this.service.getData());
}
In vscode, I encounter a compiler error
[ts] Expected 0 arguments, but got 1.
. However, when I run ng build
, it compiles correctly.
If I do not provide any arguments to the constructor, the compiler error disappears, but I receive an error when running ng build
:
error TS2554: Expected 1 argument, but got 0.
It seems like this might be an issue with vscode or one of its extensions rather than TypeScript itself.
I am using vscode insiders version 1.26.0, TypeScript insiders version 3.0.1, and Angular 6.