I'm facing a dilemma with the code snippet below. The line m.removeChild(m.childNodes[0])
is causing an issue with the TypeScript compiler.
I'm unsure if childNodes: BaseNode[];
is the correct approach in this scenario.
class BaseNode {
childNodes: BaseNode[];
constructor() {
this.childNodes = [];
}
appendChild(node: BaseNode) {
this.childNodes.push(node);
}
}
class MyNode extends BaseNode {
removeChild(node: MyNode): void {
const index = this.childNodes.indexOf(node);
if (index >= 0) {
this.childNodes.splice(index, 1);
}
}
}
const m = new MyNode();
m.appendChild(new MyNode());
// Argument of type 'BaseNode' is not assignable to parameter of type 'MyNode'.
// Property 'removeChild' is missing in type 'BaseNode' but required in type 'MyNode'.(2345)
m.removeChild(m.childNodes[0]);