Reputation: 981
class A {
constructor(name: string) {}
}
class B extends A {
constructor(name: string) {
super(name);
}
}
In typescript, is it possible for a child class to use the parent's constructor directly without redeclare the signature in the child ?
For example:
class A {
constructor(name: string) {}
}
class B extends A {}
And then we can only:
new B("name");
Upvotes: 0
Views: 140
Reputation: 52173
This should work. Based on ES6 class specification if you omit the constructor
in a sub-class, it will use a default that passes arguments to the parent constructor:
Default constructors for classes
If you don’t specify a constructor for a base class, the following definition is used:
constructor() {}
For derived classes, the following default constructor is used:
constructor(...args) { super(...args); }
(TypeScript is a superset of ES6 and follows this specification.)
Upvotes: 3