You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// 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.classRealFoo{foo(){return'real foo'}}classMockFooimplementsRealFoo{// NB: 'implements', not 'extends'foo(){return'mock foo'}}// This example works not so great:classRealBar{bar(){return'real bar'}privatep;}classMockBar1implementsRealBar{// ERROR: Property 'p' is missing in type 'MockBar1'bar(){return'mock bar'}}classMockBar2implementsRealBar{// ERROR: Types have separate declarations of private property 'p'bar(){return'mock bar'}privatep;}// This also doesn't work. Strange error, given that the 'Bar' interface doesn't have a 'p'.interfaceBarextendsRealBar{}// Automagically extract an interface from 'RealBar'classMockBar3implementsBar{// 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:
An interface can extend a class as a shortcut for getting all the class's public shape as if it was an interface.
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.
The text was updated successfully, but these errors were encountered:
TypeScript Version: nightly (2.0.0-dev.20160629)
Code
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:
interface
canextend
aclass
as a shortcut for getting all the class's public shape as if it was an interface.class
can alsoimplement
anotherclass
as if it were just an interface. It's different fromextends
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 theinterface
representation of aclass
.The text was updated successfully, but these errors were encountered: