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

Broken encapsulation in mixed classes with redeclared props #13830

Closed
tsofist opened this issue Feb 2, 2017 · 0 comments · Fixed by #13990
Closed

Broken encapsulation in mixed classes with redeclared props #13830

tsofist opened this issue Feb 2, 2017 · 0 comments · Fixed by #13990
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@tsofist
Copy link

tsofist commented Feb 2, 2017

TypeScript Version: 2.2.0-dev.20170202

#13743

Code

type Constructor<T> = new(...args: any[]) => T;

class A {
    public pb: number = 2;
    protected ptd: number = 1;
    private pvt: number = 0;
}

function mixB<T extends Constructor<{}>>(Cls: T) {
    return class extends Cls {
        protected ptd: number = 10; //override
        private pvt: number = 0; //hiding try
    };
}

function mixB2<T extends Constructor<A>>(Cls: T) {
    return class extends Cls {
        protected ptd: number = 10; //override
        //private pvt: number = 0; //hiding try failded: TS2415
    };
}

const
    AB = mixB(A),
    AB2 = mixB2(A);

function mixC<T extends Constructor<{}>>(Cls: T) {
    return class extends Cls {
        protected ptd: number = 100; //override
        private pvt: number = 0; //hiding try
    };
}

const
    AB2C = mixC(AB2),
    ABC = mixC(AB);

const
    a = new A(),
    ab = new AB(),
    abc = new ABC(),
    ab2c = new AB2C();

Expected behavior:

a.pb.toFixed();     //ok
a.ptd.toFixed();    //error: TS2445
a.pvt.toFixed();    //error: TS2341

ab.pb.toFixed();     //ok
ab.ptd.toFixed();    //error: TS2445
ab.pvt.toFixed();    //error: TS2341

abc.pb.toFixed();    //ok
abc.ptd.toFixed();   //error: TS2445
abc.pvt.toFixed();   //error: TS2341

ab2c.pb.toFixed();   //ok
ab2c.ptd.toFixed();  //error: TS2445
ab2c.pvt.toFixed();  //error: TS2341

Actual behavior:

a.pb.toFixed();     //ok
a.ptd.toFixed();    //ok error: TS2445
a.pvt.toFixed();    //ok error: TS2341

ab.pb.toFixed();     //ok
ab.ptd.toFixed();    //ok =(
ab.pvt.toFixed();    //ok =(

abc.pb.toFixed();    //ok
abc.ptd.toFixed();   //ok =(
abc.pvt.toFixed();   //ok =(

ab2c.pb.toFixed();   //ok
ab2c.ptd.toFixed();  //ok =(
ab2c.pvt.toFixed();  //ok =(
@mhegazy mhegazy added the Bug A bug in TypeScript label Feb 2, 2017
@mhegazy mhegazy added this to the TypeScript 2.2.1 milestone Feb 2, 2017
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Feb 10, 2017
@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
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants