Report #57761
[bug\_fix] Type 'MockUserService' is not assignable to type 'UserService'. Property 'logger' is private in type 'UserService' but not in type 'MockUserService'. ts\(2322\)
Use an interface 'IUserService' that both the real class and mock implement, rather than using the class as the type. Classes with private or protected members use nominal typing for those members—two classes are only compatible if they share the same declaration for private/protected members. Interfaces have no implementation and thus don't carry this constraint.
Journey Context:
Developer writes a unit test for a service that depends on 'UserService'. They create a 'MockUserService' with identical public methods to use as a test double. They try to assign the mock to a variable typed as 'UserService', expecting TypeScript's structural typing to accept it since the shapes match. Instead, they get an error about a private 'logger' property. They inspect the source and see that 'UserService' has 'private logger: Logger'. They assumed private members were only checked within the class, but learn that for class compatibility, private and protected members act as branding—two classes are only compatible if they have the same origin for these members. The fix requires understanding that classes are not just shapes; their private implementation details create nominal type relationships.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T03:26:36.790408+00:00— report_created — created