1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
tests/cases/compiler/protectedMembers.ts(40,4): error TS2445: Property 'x' is protected and only accessible within class 'C1' and its subclasses.
tests/cases/compiler/protectedMembers.ts(41,4): error TS2445: Property 'f' is protected and only accessible within class 'C1' and its subclasses.
tests/cases/compiler/protectedMembers.ts(42,4): error TS2445: Property 'sx' is protected and only accessible within class 'C1' and its subclasses.
tests/cases/compiler/protectedMembers.ts(43,4): error TS2445: Property 'sf' is protected and only accessible within class 'C1' and its subclasses.
tests/cases/compiler/protectedMembers.ts(46,4): error TS2445: Property 'x' is protected and only accessible within class 'C1' and its subclasses.
tests/cases/compiler/protectedMembers.ts(47,4): error TS2445: Property 'f' is protected and only accessible within class 'C2' and its subclasses.
tests/cases/compiler/protectedMembers.ts(48,4): error TS2445: Property 'sx' is protected and only accessible within class 'C1' and its subclasses.
tests/cases/compiler/protectedMembers.ts(49,4): error TS2445: Property 'sf' is protected and only accessible within class 'C2' and its subclasses.
tests/cases/compiler/protectedMembers.ts(68,11): error TS2446: Property 'x' is protected and only accessible through an instance of class 'C'. This is an instance of class 'A'.
tests/cases/compiler/protectedMembers.ts(69,11): error TS2446: Property 'x' is protected and only accessible through an instance of class 'C'. This is an instance of class 'B'.
tests/cases/compiler/protectedMembers.ts(97,1): error TS2322: Type 'B1' is not assignable to type 'A1'.
Property 'x' is protected but type 'B1' is not a class derived from 'A1'.
tests/cases/compiler/protectedMembers.ts(98,1): error TS2322: Type 'A1' is not assignable to type 'B1'.
Property 'x' is protected in type 'A1' but public in type 'B1'.
tests/cases/compiler/protectedMembers.ts(111,7): error TS2415: Class 'B3' incorrectly extends base class 'A3'.
Property 'x' is protected in type 'B3' but public in type 'A3'.
==== tests/cases/compiler/protectedMembers.ts (13 errors) ====
// Class with protected members
class C1 {
protected x: number;
protected static sx: number;
protected f() {
return this.x;
}
protected static sf() {
return this.sx;
}
}
// Derived class accessing protected members
class C2 extends C1 {
protected f() {
return super.f() + this.x;
}
protected static sf() {
return super.sf() + this.sx;
}
}
// Derived class making protected members public
class C3 extends C2 {
x: number;
static sx: number;
f() {
return super.f();
}
static sf() {
return super.sf();
}
}
var c1: C1;
var c2: C2;
var c3: C3;
// All of these should be errors
c1.x;
~
!!! error TS2445: Property 'x' is protected and only accessible within class 'C1' and its subclasses.
c1.f();
~
!!! error TS2445: Property 'f' is protected and only accessible within class 'C1' and its subclasses.
C1.sx;
~~
!!! error TS2445: Property 'sx' is protected and only accessible within class 'C1' and its subclasses.
C1.sf();
~~
!!! error TS2445: Property 'sf' is protected and only accessible within class 'C1' and its subclasses.
// All of these should be errors
c2.x;
~
!!! error TS2445: Property 'x' is protected and only accessible within class 'C1' and its subclasses.
c2.f();
~
!!! error TS2445: Property 'f' is protected and only accessible within class 'C2' and its subclasses.
C2.sx;
~~
!!! error TS2445: Property 'sx' is protected and only accessible within class 'C1' and its subclasses.
C2.sf();
~~
!!! error TS2445: Property 'sf' is protected and only accessible within class 'C2' and its subclasses.
// All of these should be ok
c3.x;
c3.f();
C3.sx;
C3.sf();
class A {
protected x;
}
class B extends A {
y;
}
class C extends A {
z;
static foo(a: A, b: B, c: C, d: D, e: E) {
a.x = 1; // Error, access must be through C or type derived from C
~
!!! error TS2446: Property 'x' is protected and only accessible through an instance of class 'C'. This is an instance of class 'A'.
b.x = 1; // Error, access must be through C or type derived from C
~
!!! error TS2446: Property 'x' is protected and only accessible through an instance of class 'C'. This is an instance of class 'B'.
c.x = 1;
d.x = 1;
e.x = 1;
}
}
class D extends C {
d;
}
interface E extends C {
e;
}
class CC {
protected constructor() {
}
}
class A1 {
protected x;
}
class B1 {
x;
}
var a1: A1;
var b1: B1;
a1 = b1; // Error, B1 doesn't derive from A1
~~
!!! error TS2322: Type 'B1' is not assignable to type 'A1'.
!!! error TS2322: Property 'x' is protected but type 'B1' is not a class derived from 'A1'.
b1 = a1; // Error, x is protected in A1 but public in B1
~~
!!! error TS2322: Type 'A1' is not assignable to type 'B1'.
!!! error TS2322: Property 'x' is protected in type 'A1' but public in type 'B1'.
class A2 {
protected x;
}
class B2 extends A2 {
x;
}
class A3 {
x;
}
// Error x is protected in B3 but public in A3
class B3 extends A3 {
~~
!!! error TS2415: Class 'B3' incorrectly extends base class 'A3'.
!!! error TS2415: Property 'x' is protected in type 'B3' but public in type 'A3'.
protected x;
}
|