Skip to content

Suggestion: Permit an implementing class to ignore private methods of the implementee class #471

Closed
@NoelAbrahams

Description

@NoelAbrahams

Hi, we have a requirement to create mocks of real classes defined in code. The exact requirements are

  • A specific mock should define the public API of the real class.
  • If the public API of the real class changes in any way then the compiler should detect the corresponding breakage in the mock class.

A possible solution might be as follows:

declare var fileWriter: any; 

// Real class
class Foo {

    public writeToFile(){
        fileWriter.writeToFile('');
    }
}

// Mock
class MockFoo implements Foo {

     public writeToFile(){
         // do nothing
     }
}

This appears to solve the problem because changing Foo.writeToFile will trigger a compilation error along the lines of "Class MockFoo declares interface Foo but does not implement it..."

The problem with this approach is that class Foo is not permitted to have any private methods or fields. If we were to add a private method foo() to class Foo then it's no longer possible for MockFoo to implement Foo because the compiler doesn't permit it.

I suggest that when a class implements another class the implementing class be allowed to ignore the private fields and methods (both static and instance) of the implementee.

(I am aware that there are workarounds, such as declaring an interface that both Foo and MockFoo implement, but that introduces an unnecessary maintenance overhead.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript visionRevisitAn issue worth coming back toSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions