Skip to content

Broken encapsulation in mixed classes with redeclared props #13830

Closed
@tsofist

Description

@tsofist

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 =(

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions