Within my code, I have a parent class where I am creating a function called store-relation and a child class that defines a table in the store. The issue arises when I try to connect the save()
function from the parent class to the child's table which is not defined until later. I attempted to solve this by passing the child's tableName
attribute to the parent's constructor, but my linter flagged an error stating that super
must be called before referencing this
.
Here is the snippet of the parent class:
export class StoredObject {
constructor(private tableName: string) {}
public save() {
store.collection(this.tableName) //...
}
}
And here is the child class:
export class Inventory extends StoredObject {
tableName = "inventory";
constructor(
public myVar //...
) {
super(this.tableName)
}
}
I understand that one solution is to use a getter function to return the value of this.tableName
, but that feels like a workaround. Another option would be to hardcode the string directly into super
(e.g., in the Inventory
constructor, using { super("inventory") }
), but that approach seems less elegant. It seems like there should be a more conventional way to achieve what I'm attempting to do, but so far I haven't been able to find it.