Skip to content

Cant satisfy complier on calls x implements y, where y is class. #5571

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

Closed
shlomiassaf opened this issue Nov 9, 2015 · 4 comments
Closed
Labels
Question An issue which isn't directly actionable in code

Comments

@shlomiassaf
Copy link

Hi,

Typescript 1.6.2
When trying to implement a class with private variables complier wants the derived class to implement the parent's private variables.

export class Father {
    private hidden: string;
    public visible: string;
}

export  class Child implements Father{
    public visible: string;
}

(7,15): error TS2420: Class 'child' incorrectly implements interface 'father'.
Property 'hidden' is missing in type 'child'.

Trying to do the wrong thing and do implement the private variable results in the compiler not happy since we implement a private variable of the parent (In a typed language this is wrong as well, but in JS it might be a good catch...)

export class Father {
    private hidden: string;
    public visible: string;
}

export  class Child implements Father{
    private hidden: string;
    public visible: string;

}

(7,15): error TS2420: Class 'child' incorrectly implements interface 'father'.
Types have separate declarations of a private property 'hidden'.

@sandersn
Copy link
Member

sandersn commented Nov 9, 2015

Can you try extends instead of implements ? implements is for interfaces.

@shlomiassaf
Copy link
Author

I can try and it will probably work, but i'm trying to do some "mixin" stuff

Typescript has an official documented approach for it, https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Mixins.md

I'm sure I've seen some other places in the docs that "implement" concrete classes...
So it should be possible

@mhegazy
Copy link
Contributor

mhegazy commented Nov 9, 2015

the issue here is the private property declaration. When implementing a class with private, the implementing class gets the property declaration but not its implementation. private members can not be re-implemented, the only allowed implementation for them is their original declaration. so now you have a class is not implementable.
for mix-in patterns, do not use private members, or split your non-private declarations in an interface and use that.

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Nov 9, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Nov 9, 2015

see #1101 for more details.

@mhegazy mhegazy closed this as completed Nov 9, 2015
@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
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants