Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use a class like an interface if has private members #9433

Closed
yortus opened this issue Jun 30, 2016 · 1 comment
Closed

Can't use a class like an interface if has private members #9433

yortus opened this issue Jun 30, 2016 · 1 comment

Comments

@yortus
Copy link
Contributor

yortus commented Jun 30, 2016

TypeScript Version: nightly (2.0.0-dev.20160629)

Code

// This example works great. The 'MockFoo' implementation must have
// the same shape as 'RealFoo', but shares no implementation. We get
// compile-time errors if any members are missing or incompatible.
// This saves the boilerplate of separately maintaining a 'Foo' interface.
class RealFoo {
    foo() { return 'real foo'}
}
class MockFoo implements RealFoo { // NB: 'implements', not 'extends'
    foo() { return 'mock foo'}
}


// This example works not so great:
class RealBar {
    bar() { return 'real bar'}
    private p;
}
class MockBar1 implements RealBar { // ERROR: Property 'p' is missing in type 'MockBar1'
    bar() { return 'mock bar'}
}
class MockBar2 implements RealBar { // ERROR: Types have separate declarations of private property 'p'
    bar() { return 'mock bar'}
    private p;
}


// This also doesn't work. Strange error, given that the 'Bar' interface doesn't have a 'p'.
interface Bar extends RealBar { } // Automagically extract an interface from 'RealBar'
class MockBar3 implements Bar { // ERROR: Property 'p' is missing in type 'MockBar3'
    bar() { return 'mock bar'}
}

Expected behavior:
I expected all the examples above to compile without errors.

Actual behavior:
Compile-time errors are issued as shown in the code comments above.

Additional Info:
I should note that I'm using a technique above that I'm not 100% sure is officially sanctioned, even though it works beautifully as in the first example:

  1. An interface can extend a class as a shortcut for getting all the class's public shape as if it was an interface.
  2. In addition to (1), a class can also implement another class as if it were just an interface. It's different from extends in that it's purely compile time, and not runtime implementation is shared with the 'implemented' class.

In my case, that second 'feature' saves a substantial amount of boilerplate work associated with maintaining a separate interface.

The problem, as noted in the code comments, is that if the class has private members, then these members seem to prevent it from being used like an interface. I don't know why the private members matter - I though they should just be omitted when looking at the interface representation of a class.

@yortus
Copy link
Contributor Author

yortus commented Jun 30, 2016

After deeper digging, this is a duplicate of #471 (coincidentally duplicated right down to the MockFoo in the example).

#471 is closed, but tagged 'revisit'. I do hope it can be revisited with a workable solution.

@yortus yortus closed this as completed Jun 30, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant