Just starting out with class-based programming, I've been tinkering with a TypeScript API.
Here's the scenario:
import { Router } from "express";
export default class BaseController {
public router = Router();
}
and then I create another class that extends the previous one:
import BaseController from "./BaseController";
export default FooController extends BaseController {
constuctor() {
// Further explanation on this super call will be provided later in the question
super();
this.router.get("/", this.faz);
}
private faz(req: Request, res: Response) {
res.status(200).send("boo!");
}
}
If I omit super();
, my code won't compile and throws this error message:
Constructors for derived classes must contain a 'super' call.
I understand that super
is used to invoke the parent class constructor, but since my parent class doesn't have a constructor, it feels like I'm essentially calling an empty function.
Question: Why is it necessary to use super();
in a derived class if the parent class lacks a constructor? (So basically, am I not really calling anything?)