When mocking classes in TypeScript, I’ve found that the class type includes both private and # members. My mocks for the public members of the class don’t satisfy the class.
class AClass {
#a: string = "A";
public A() {
return this.#a;
}
private B() {
return "B";
}
}
// ERROR: Type '{ A: () => string; }' is missing the following properties from type 'AClass': #a, B ts(2739)
const mockAClass: AClass = {
A: () => "Mock A"
};
I can get around it by creating an “interface type” like this, and changing the function expecting AClass to expect IAClass instead:
The “I” prefix is common in C# but I haven’t really seen this in anywhere in TypeScript. Is there a best practice for naming interface like types in TypeScript?
The classes I’m mocking have names like ServiceClient, UsersTable, etc. Before I start making interface types like IServiceClient and IUsersTable, or ServiceClientInterface & UsersTableInterface I wanted to check if there is a recommended naming convention in TypeScript.
Or perhaps there is a better TypeScript solution for this like hiding the private members from the class somehow?