Skip to content

Commit 2552560

Browse files
committed
Test that protected constructors are accessible
in static methods of subclasses
1 parent 046e9ea commit 2552560

6 files changed

+135
-45
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(26,28): error TS2674: Constructor of class 'BaseB' is protected and only accessible within the class declaration.
2-
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(29,24): error TS2675: Cannot extend a class 'BaseC'. Class constructor is marked as private.
3-
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(32,28): error TS2673: Constructor of class 'BaseC' is private and only accessible within the class declaration.
4-
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(36,10): error TS2674: Constructor of class 'BaseB' is protected and only accessible within the class declaration.
5-
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(37,10): error TS2673: Constructor of class 'BaseC' is private and only accessible within the class declaration.
1+
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(28,28): error TS2674: Constructor of class 'BaseB' is protected and only accessible within the class declaration.
2+
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(32,24): error TS2675: Cannot extend a class 'BaseC'. Class constructor is marked as private.
3+
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(35,28): error TS2673: Constructor of class 'BaseC' is private and only accessible within the class declaration.
4+
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(36,35): error TS2673: Constructor of class 'BaseC' is private and only accessible within the class declaration.
5+
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(40,10): error TS2674: Constructor of class 'BaseB' is protected and only accessible within the class declaration.
6+
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(41,10): error TS2673: Constructor of class 'BaseC' is private and only accessible within the class declaration.
67

78

8-
==== tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts (5 errors) ====
9+
==== tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts (6 errors) ====
910

1011
class BaseA {
1112
public constructor(public x: number) { }
@@ -14,35 +15,41 @@ tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessib
1415

1516
class BaseB {
1617
protected constructor(public x: number) { }
17-
createInstance() { new BaseB(1); }
18+
createInstance() { new BaseB(2); }
1819
}
1920

2021
class BaseC {
21-
private constructor(public x: number) { }
22-
createInstance() { new BaseC(1); }
22+
private constructor(public x: number) { }
23+
createInstance() { new BaseC(3); }
24+
static staticInstance() { new BaseC(4); }
2325
}
2426

2527
class DerivedA extends BaseA {
2628
constructor(public x: number) { super(x); }
27-
createInstance() { new DerivedA(1); }
28-
createBaseInstance() { new BaseA(1); }
29+
createInstance() { new DerivedA(5); }
30+
createBaseInstance() { new BaseA(6); }
31+
static staticBaseInstance() { new BaseA(7); }
2932
}
3033

3134
class DerivedB extends BaseB {
3235
constructor(public x: number) { super(x); }
33-
createInstance() { new DerivedB(1); }
34-
createBaseInstance() { new BaseB(1); } // error
36+
createInstance() { new DerivedB(7); }
37+
createBaseInstance() { new BaseB(8); } // error
3538
~~~~~~~~~~~~
3639
!!! error TS2674: Constructor of class 'BaseB' is protected and only accessible within the class declaration.
40+
static staticBaseInstance() { new BaseB(9); } // ok
3741
}
3842

3943
class DerivedC extends BaseC { // error
4044
~~~~~
4145
!!! error TS2675: Cannot extend a class 'BaseC'. Class constructor is marked as private.
4246
constructor(public x: number) { super(x); }
43-
createInstance() { new DerivedC(1); }
44-
createBaseInstance() { new BaseC(1); } // error
45-
~~~~~~~~~~~~
47+
createInstance() { new DerivedC(9); }
48+
createBaseInstance() { new BaseC(10); } // error
49+
~~~~~~~~~~~~~
50+
!!! error TS2673: Constructor of class 'BaseC' is private and only accessible within the class declaration.
51+
static staticBaseInstance() { new BaseC(11); } // error
52+
~~~~~~~~~~~~~
4653
!!! error TS2673: Constructor of class 'BaseC' is private and only accessible within the class declaration.
4754
}
4855

@@ -56,4 +63,5 @@ tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessib
5663

5764
var da = new DerivedA(1);
5865
var db = new DerivedB(1);
59-
var dc = new DerivedC(1);
66+
var dc = new DerivedC(1);
67+

tests/baselines/reference/classConstructorAccessibility2.js

+31-18
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,34 @@ class BaseA {
77

88
class BaseB {
99
protected constructor(public x: number) { }
10-
createInstance() { new BaseB(1); }
10+
createInstance() { new BaseB(2); }
1111
}
1212

1313
class BaseC {
14-
private constructor(public x: number) { }
15-
createInstance() { new BaseC(1); }
14+
private constructor(public x: number) { }
15+
createInstance() { new BaseC(3); }
16+
static staticInstance() { new BaseC(4); }
1617
}
1718

1819
class DerivedA extends BaseA {
1920
constructor(public x: number) { super(x); }
20-
createInstance() { new DerivedA(1); }
21-
createBaseInstance() { new BaseA(1); }
21+
createInstance() { new DerivedA(5); }
22+
createBaseInstance() { new BaseA(6); }
23+
static staticBaseInstance() { new BaseA(7); }
2224
}
2325

2426
class DerivedB extends BaseB {
2527
constructor(public x: number) { super(x); }
26-
createInstance() { new DerivedB(1); }
27-
createBaseInstance() { new BaseB(1); } // error
28+
createInstance() { new DerivedB(7); }
29+
createBaseInstance() { new BaseB(8); } // error
30+
static staticBaseInstance() { new BaseB(9); } // ok
2831
}
2932

3033
class DerivedC extends BaseC { // error
3134
constructor(public x: number) { super(x); }
32-
createInstance() { new DerivedC(1); }
33-
createBaseInstance() { new BaseC(1); } // error
35+
createInstance() { new DerivedC(9); }
36+
createBaseInstance() { new BaseC(10); } // error
37+
static staticBaseInstance() { new BaseC(11); } // error
3438
}
3539

3640
var ba = new BaseA(1);
@@ -39,7 +43,8 @@ var bc = new BaseC(1); // error
3943

4044
var da = new DerivedA(1);
4145
var db = new DerivedB(1);
42-
var dc = new DerivedC(1);
46+
var dc = new DerivedC(1);
47+
4348

4449
//// [classConstructorAccessibility2.js]
4550
var __extends = (this && this.__extends) || function (d, b) {
@@ -58,14 +63,15 @@ var BaseB = (function () {
5863
function BaseB(x) {
5964
this.x = x;
6065
}
61-
BaseB.prototype.createInstance = function () { new BaseB(1); };
66+
BaseB.prototype.createInstance = function () { new BaseB(2); };
6267
return BaseB;
6368
}());
6469
var BaseC = (function () {
6570
function BaseC(x) {
6671
this.x = x;
6772
}
68-
BaseC.prototype.createInstance = function () { new BaseC(1); };
73+
BaseC.prototype.createInstance = function () { new BaseC(3); };
74+
BaseC.staticInstance = function () { new BaseC(4); };
6975
return BaseC;
7076
}());
7177
var DerivedA = (function (_super) {
@@ -74,8 +80,9 @@ var DerivedA = (function (_super) {
7480
_super.call(this, x);
7581
this.x = x;
7682
}
77-
DerivedA.prototype.createInstance = function () { new DerivedA(1); };
78-
DerivedA.prototype.createBaseInstance = function () { new BaseA(1); };
83+
DerivedA.prototype.createInstance = function () { new DerivedA(5); };
84+
DerivedA.prototype.createBaseInstance = function () { new BaseA(6); };
85+
DerivedA.staticBaseInstance = function () { new BaseA(7); };
7986
return DerivedA;
8087
}(BaseA));
8188
var DerivedB = (function (_super) {
@@ -84,8 +91,9 @@ var DerivedB = (function (_super) {
8491
_super.call(this, x);
8592
this.x = x;
8693
}
87-
DerivedB.prototype.createInstance = function () { new DerivedB(1); };
88-
DerivedB.prototype.createBaseInstance = function () { new BaseB(1); }; // error
94+
DerivedB.prototype.createInstance = function () { new DerivedB(7); };
95+
DerivedB.prototype.createBaseInstance = function () { new BaseB(8); }; // error
96+
DerivedB.staticBaseInstance = function () { new BaseB(9); }; // ok
8997
return DerivedB;
9098
}(BaseB));
9199
var DerivedC = (function (_super) {
@@ -94,8 +102,9 @@ var DerivedC = (function (_super) {
94102
_super.call(this, x);
95103
this.x = x;
96104
}
97-
DerivedC.prototype.createInstance = function () { new DerivedC(1); };
98-
DerivedC.prototype.createBaseInstance = function () { new BaseC(1); }; // error
105+
DerivedC.prototype.createInstance = function () { new DerivedC(9); };
106+
DerivedC.prototype.createBaseInstance = function () { new BaseC(10); }; // error
107+
DerivedC.staticBaseInstance = function () { new BaseC(11); }; // error
99108
return DerivedC;
100109
}(BaseC));
101110
var ba = new BaseA(1);
@@ -121,24 +130,28 @@ declare class BaseC {
121130
x: number;
122131
private constructor(x);
123132
createInstance(): void;
133+
static staticInstance(): void;
124134
}
125135
declare class DerivedA extends BaseA {
126136
x: number;
127137
constructor(x: number);
128138
createInstance(): void;
129139
createBaseInstance(): void;
140+
static staticBaseInstance(): void;
130141
}
131142
declare class DerivedB extends BaseB {
132143
x: number;
133144
constructor(x: number);
134145
createInstance(): void;
135146
createBaseInstance(): void;
147+
static staticBaseInstance(): void;
136148
}
137149
declare class DerivedC extends BaseC {
138150
x: number;
139151
constructor(x: number);
140152
createInstance(): void;
141153
createBaseInstance(): void;
154+
static staticBaseInstance(): void;
142155
}
143156
declare var ba: BaseA;
144157
declare var bb: any;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility5.ts(9,21): error TS2674: Constructor of class 'Base' is protected and only accessible within the class declaration.
2+
3+
4+
==== tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility5.ts (1 errors) ====
5+
class Base {
6+
protected constructor() { }
7+
}
8+
class Derived extends Base {
9+
static make() { new Base() } // ok
10+
}
11+
12+
class Unrelated {
13+
static fake() { new Base() } // error
14+
~~~~~~~~~~
15+
!!! error TS2674: Constructor of class 'Base' is protected and only accessible within the class declaration.
16+
}
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//// [classConstructorAccessibility5.ts]
2+
class Base {
3+
protected constructor() { }
4+
}
5+
class Derived extends Base {
6+
static make() { new Base() } // ok
7+
}
8+
9+
class Unrelated {
10+
static fake() { new Base() } // error
11+
}
12+
13+
14+
//// [classConstructorAccessibility5.js]
15+
var __extends = (this && this.__extends) || function (d, b) {
16+
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
17+
function __() { this.constructor = d; }
18+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
19+
};
20+
var Base = (function () {
21+
function Base() {
22+
}
23+
return Base;
24+
}());
25+
var Derived = (function (_super) {
26+
__extends(Derived, _super);
27+
function Derived() {
28+
_super.apply(this, arguments);
29+
}
30+
Derived.make = function () { new Base(); }; // ok
31+
return Derived;
32+
}(Base));
33+
var Unrelated = (function () {
34+
function Unrelated() {
35+
}
36+
Unrelated.fake = function () { new Base(); }; // error
37+
return Unrelated;
38+
}());

tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,34 @@ class BaseA {
77

88
class BaseB {
99
protected constructor(public x: number) { }
10-
createInstance() { new BaseB(1); }
10+
createInstance() { new BaseB(2); }
1111
}
1212

1313
class BaseC {
14-
private constructor(public x: number) { }
15-
createInstance() { new BaseC(1); }
14+
private constructor(public x: number) { }
15+
createInstance() { new BaseC(3); }
16+
static staticInstance() { new BaseC(4); }
1617
}
1718

1819
class DerivedA extends BaseA {
1920
constructor(public x: number) { super(x); }
20-
createInstance() { new DerivedA(1); }
21-
createBaseInstance() { new BaseA(1); }
21+
createInstance() { new DerivedA(5); }
22+
createBaseInstance() { new BaseA(6); }
23+
static staticBaseInstance() { new BaseA(7); }
2224
}
2325

2426
class DerivedB extends BaseB {
2527
constructor(public x: number) { super(x); }
26-
createInstance() { new DerivedB(1); }
27-
createBaseInstance() { new BaseB(1); } // error
28+
createInstance() { new DerivedB(7); }
29+
createBaseInstance() { new BaseB(8); } // error
30+
static staticBaseInstance() { new BaseB(9); } // ok
2831
}
2932

3033
class DerivedC extends BaseC { // error
3134
constructor(public x: number) { super(x); }
32-
createInstance() { new DerivedC(1); }
33-
createBaseInstance() { new BaseC(1); } // error
35+
createInstance() { new DerivedC(9); }
36+
createBaseInstance() { new BaseC(10); } // error
37+
static staticBaseInstance() { new BaseC(11); } // error
3438
}
3539

3640
var ba = new BaseA(1);
@@ -39,4 +43,4 @@ var bc = new BaseC(1); // error
3943

4044
var da = new DerivedA(1);
4145
var db = new DerivedB(1);
42-
var dc = new DerivedC(1);
46+
var dc = new DerivedC(1);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Base {
2+
protected constructor() { }
3+
}
4+
class Derived extends Base {
5+
static make() { new Base() } // ok
6+
}
7+
8+
class Unrelated {
9+
static fake() { new Base() } // error
10+
}

0 commit comments

Comments
 (0)