How can I create an abstract constructor for an abstract class?
For example, if I have this abstract class:
export abstract class Foo<T> extends Bar<T> {
constructor(someParam: any) {
super();
this.someObj = someParam;
}
}
Then it is consumed by this class:
export class FooImpl extends Foo<SomeType> {
}
If I don't override the constructor then the default parameterless constructor will be used on the FooImpl
class which will lead to an improper state within the class.
How can I define an abstract constructor or force consumers of the abstract class to override the constructor when Foo<T>
is consumed?
EDIT
The exact code I have is below and would appear that this is somehow related to vscode
export abstract class FireStoreDataSource<T> extends DataSource<T> {
constructor(store: () => Observable<T[]>) {
super();
}
}
Then I am extending this class as follows
export class AssessmentCenterDataSource extends FireStoreDataSource<AssessmentCenter> {
}
Then creating an instance in an angular component ngOnInit
ngOnInit() {
this.dataSource = new AssessmentCenterDataSource(() => this.service.getData());
}
Here in vscode I am getting the compiler error [ts] Expected 0 arguments, but got 1.
, however when I run ng build
it does build correctly
If I do not pass anything to the constructor then the compiler error goes away and I get an error when I run ng build
which is error TS2554: Expected 1 arguments, but got 0.
So it looks like this might be an issue with vscode or one of the addons I have installed rather than typescript
I am using vscode insiders 1.26.0 and typescript insiders 3.0.1 along with Angular 6
class
syntax. If you added a constructor to the subclass and didn't callsuper
, it would be an error. – T.J. Crowder