TypeScript中类实现含私有函数的类时的报错问题咨询
implements in TypeScript (And How to Fix It) Let’s break down exactly what’s going on here—this is a super common point of confusion with TypeScript’s implements keyword, so you’re not alone!
First, a critical rule to hammer home: implements is built for interfaces, not classes. When you write class B implements A where A is a class, TypeScript treats A as a structural type (checking that B has all the same members as A), but it can’t handle private members correctly—and that’s the root of your errors.
Why Your Errors Keep Happening
Let’s walk through your scenario step by step:
First error:
property 'f' is missing in type 'B'
Your classAhas a memberf(I’m guessing it’s private, since addingpublic ftoBcaused an access modifier mismatch). When you useimplements A, TypeScript expectsBto mirror every member ofA—including private ones. But private members are scoped exclusively to the class they’re defined in. Even if you addprivate ftoB, it’s a completely separate private member fromA’sf—TypeScript picks up on this mismatch and throws the second error.The tool tip suggesting
extendsisn’t a random guess—it’s the right approach for what you’re probably trying to do.extendsis for class inheritance: it letsBinherit all members (including private ones, though you can’t access them directly inB) and implementation logic fromA, which is exactly what you’d want if you’re trying to "adopt" another class’s behavior.
Fixes for Your Scenario
Choose the solution that matches your actual goal:
1. Use extends if you want to inherit behavior/members from A
If your goal is to reuse A’s code or establish a subclass relationship, swap implements for extends:
class A { private f: string; constructor() { this.f = "hello"; } public doSomething() { console.log(this.f); } } // This works perfectly—B inherits all of A's members and logic class B extends A { // Add your custom logic here public doAnotherThing() { console.log("Doing something extra!"); } }
2. Use an interface if you only need structural compatibility
If you just want B to match the public structure of A (without inheriting implementation), extract an interface from A and have both classes implement it:
// Define the public structure as an interface interface AContract { doSomething(): void; } class A implements AContract { private f: string; constructor() { this.f = "hello"; } public doSomething() { console.log(this.f); } } // Now B can implement the interface without worrying about A's private members class B implements AContract { public doSomething() { console.log("B's own version of doSomething"); } }
3. Ditch private members if you must use implements
If you absolutely need to use implements (e.g., for multiple "implementations"), make sure all members in A are either public or protected—private members can’t be implemented by another class, full stop. Their scope is tied directly to the original class, so no other class can replicate that.
Key Takeaway
implements= match a public structure (use interfaces for this)extends= inherit from a class (reuse code, establish a parent-child relationship)
Private members lock a class into being un-implementable viaimplements, because they’re not part of a public structure—they’re exclusive to the class they belong to.
内容的提问来源于stack exchange,提问作者Radu Szasz

