I was exploring the possibility of having a class implementing a class in TypeScript.
Hence, I wrote the following code Playground link:
class A {
private f() { console.log("f"); }
public g() { console.log("G"); }
}
class B implements A {
public g() { console.log("g"); }
}
And I got the error: Class 'B' incorrectly implements class 'A' --- property 'f' is missing in type 'B'
coupled with a suggestion that I actually meant extends
.
So I tried to make a private field called f
(public
didn't work as it detects they have different access modifiers) Playground link
Now I get the error: Class 'B' incorrectly implements class 'A'. Types have separate declarations of a private property 'f'
; this leaves me very confused:
- why do private members even matter - if I implement the same algorithm using different data structures, will I have to declare something named the same just for the sake of type checking?
- why do I get the error when implementing
f
as a private function?
I wouldn't do this in practice, but I am curious about why TS works like this.
Thanks!