Skip to content

Commit 88961a2

Browse files
committed
Add tests
1 parent 43c49b1 commit 88961a2

File tree

2 files changed

+196
-0
lines changed

2 files changed

+196
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Repro from #13830
2+
3+
type Constructor<T> = new(...args: any[]) => T;
4+
5+
class A {
6+
public pb: number = 2;
7+
protected ptd: number = 1;
8+
private pvt: number = 0;
9+
}
10+
11+
function mixB<T extends Constructor<{}>>(Cls: T) {
12+
return class extends Cls {
13+
protected ptd: number = 10;
14+
private pvt: number = 0;
15+
};
16+
}
17+
18+
function mixB2<T extends Constructor<A>>(Cls: T) {
19+
return class extends Cls {
20+
protected ptd: number = 10;
21+
};
22+
}
23+
24+
const
25+
AB = mixB(A),
26+
AB2 = mixB2(A);
27+
28+
function mixC<T extends Constructor<{}>>(Cls: T) {
29+
return class extends Cls {
30+
protected ptd: number = 100;
31+
private pvt: number = 0;
32+
};
33+
}
34+
35+
const
36+
AB2C = mixC(AB2),
37+
ABC = mixC(AB);
38+
39+
const
40+
a = new A(),
41+
ab = new AB(),
42+
abc = new ABC(),
43+
ab2c = new AB2C();
44+
45+
a.pb.toFixed();
46+
a.ptd.toFixed(); // Error
47+
a.pvt.toFixed(); // Error
48+
49+
ab.pb.toFixed();
50+
ab.ptd.toFixed(); // Error
51+
ab.pvt.toFixed(); // Error
52+
53+
abc.pb.toFixed();
54+
abc.ptd.toFixed(); // Error
55+
abc.pvt.toFixed(); // Error
56+
57+
ab2c.pb.toFixed();
58+
ab2c.ptd.toFixed(); // Error
59+
ab2c.pvt.toFixed(); // Error
60+
61+
// Repro from #13924
62+
63+
class Person {
64+
constructor(public name: string) {}
65+
66+
protected myProtectedFunction() {
67+
// do something
68+
}
69+
}
70+
71+
function PersonMixin<T extends Constructor<Person>>(Base: T) {
72+
return class extends Base {
73+
constructor(...args: any[]) {
74+
super(...args);
75+
}
76+
77+
myProtectedFunction() {
78+
super.myProtectedFunction();
79+
// do more things
80+
}
81+
};
82+
}
83+
84+
class Customer extends PersonMixin(Person) {
85+
accountBalance: number;
86+
f() {
87+
}
88+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// @declaration: true
2+
3+
type Constructable = new (...args: any[]) => object;
4+
5+
class Private {
6+
constructor (...args: any[]) {}
7+
private p: string;
8+
}
9+
10+
class Private2 {
11+
constructor (...args: any[]) {}
12+
private p: string;
13+
}
14+
15+
class Protected {
16+
constructor (...args: any[]) {}
17+
protected p: string;
18+
protected static s: string;
19+
}
20+
21+
class Protected2 {
22+
constructor (...args: any[]) {}
23+
protected p: string;
24+
protected static s: string;
25+
}
26+
27+
class Public {
28+
constructor (...args: any[]) {}
29+
public p: string;
30+
public static s: string;
31+
}
32+
33+
class Public2 {
34+
constructor (...args: any[]) {}
35+
public p: string;
36+
public static s: string;
37+
}
38+
39+
function f1(x: Private & Private2) {
40+
x.p; // Error, private constituent makes property inaccessible
41+
}
42+
43+
function f2(x: Private & Protected) {
44+
x.p; // Error, private constituent makes property inaccessible
45+
}
46+
47+
function f3(x: Private & Public) {
48+
x.p; // Error, private constituent makes property inaccessible
49+
}
50+
51+
function f4(x: Protected & Protected2) {
52+
x.p; // Error, protected when all constituents are protected
53+
}
54+
55+
function f5(x: Protected & Public) {
56+
x.p; // Ok, public if any constituent is public
57+
}
58+
59+
function f6(x: Public & Public2) {
60+
x.p; // Ok, public if any constituent is public
61+
}
62+
63+
declare function Mix<T, U>(c1: T, c2: U): T & U;
64+
65+
// Can't derive from type with inaccessible properties
66+
67+
class C1 extends Mix(Private, Private2) {}
68+
class C2 extends Mix(Private, Protected) {}
69+
class C3 extends Mix(Private, Public) {}
70+
71+
class C4 extends Mix(Protected, Protected2) {
72+
f(c4: C4, c5: C5, c6: C6) {
73+
c4.p;
74+
c5.p;
75+
c6.p;
76+
}
77+
static g() {
78+
C4.s;
79+
C5.s;
80+
C6.s
81+
}
82+
}
83+
84+
class C5 extends Mix(Protected, Public) {
85+
f(c4: C4, c5: C5, c6: C6) {
86+
c4.p; // Error, not in class deriving from Protected2
87+
c5.p;
88+
c6.p;
89+
}
90+
static g() {
91+
C4.s; // Error, not in class deriving from Protected2
92+
C5.s;
93+
C6.s
94+
}
95+
}
96+
97+
class C6 extends Mix(Public, Public2) {
98+
f(c4: C4, c5: C5, c6: C6) {
99+
c4.p; // Error, not in class deriving from Protected2
100+
c5.p;
101+
c6.p;
102+
}
103+
static g() {
104+
C4.s; // Error, not in class deriving from Protected2
105+
C5.s;
106+
C6.s
107+
}
108+
}

0 commit comments

Comments
 (0)