I have a CPU-constrained project with a large number of classes and I use interfaces to minimize the number of imports I use in each module. However, I've run into some issues when declaring private methods on classes that implement their respective interfaces. Currently, I'm encountering:
index.d.ts
:
interface IColony {
// (other public properties)
// instantiateVirtualComponents(): void;
}
Colony.ts
:
export class Colony implements IColony {
// (constructor and other methods)
private instantiateVirtualComponents(): void {
// (implementation)
}
}
The issue is that TypeScript won't seem to let me declare private properties on this class. As is, tsc complains:
Error:(398, 3) TS2322:Type 'IColony' is not assignable to type 'Colony'. Property 'instantiateVirtualComponents' is missing in type 'IColony'.
I'm pretty sure that private properties and methods shouldn't be declared in the interface, but just to be safe, if I uncomment the method declaration in IColony
, tsc then complains the following (among lots of other errors resulting from this):
Error:(10, 14) TS2420:Class 'Colony' incorrectly implements interface 'IColony'. Property 'instantiateVirtualComponents' is private in type 'Colony' but not in type 'IColony'.
What am I doing wrong here? Can you simply not declare private members on classes that implement an interface?
For reference, Colony.ts
is here and the relevant part of index.d.ts
is here.