|
| 1 | +tests/cases/compiler/protectedMembersThisParameter.ts(9,9): error TS2445: Property 'secret' is protected and only accessible within class 'Message' and its subclasses. |
| 2 | +tests/cases/compiler/protectedMembersThisParameter.ts(30,7): error TS2445: Property 'b' is protected and only accessible within class 'B' and its subclasses. |
| 3 | +tests/cases/compiler/protectedMembersThisParameter.ts(41,7): error TS2446: Property 'a' is protected and only accessible through an instance of class 'C'. This is an instance of class 'B'. |
| 4 | +tests/cases/compiler/protectedMembersThisParameter.ts(42,7): error TS2445: Property 'b' is protected and only accessible within class 'B' and its subclasses. |
| 5 | +tests/cases/compiler/protectedMembersThisParameter.ts(46,7): error TS2445: Property 'a' is protected and only accessible within class 'A' and its subclasses. |
| 6 | +tests/cases/compiler/protectedMembersThisParameter.ts(47,7): error TS2445: Property 'b' is protected and only accessible within class 'B' and its subclasses. |
| 7 | +tests/cases/compiler/protectedMembersThisParameter.ts(51,7): error TS2445: Property 'a' is protected and only accessible within class 'A' and its subclasses. |
| 8 | +tests/cases/compiler/protectedMembersThisParameter.ts(52,7): error TS2445: Property 'b' is protected and only accessible within class 'B' and its subclasses. |
| 9 | +tests/cases/compiler/protectedMembersThisParameter.ts(55,7): error TS2445: Property 'a' is protected and only accessible within class 'A' and its subclasses. |
| 10 | +tests/cases/compiler/protectedMembersThisParameter.ts(56,7): error TS2445: Property 'b' is protected and only accessible within class 'B' and its subclasses. |
| 11 | +tests/cases/compiler/protectedMembersThisParameter.ts(64,9): error TS2445: Property 'd1' is protected and only accessible within class 'D1' and its subclasses. |
| 12 | +tests/cases/compiler/protectedMembersThisParameter.ts(68,9): error TS2445: Property 'd1' is protected and only accessible within class 'D1' and its subclasses. |
| 13 | +tests/cases/compiler/protectedMembersThisParameter.ts(76,9): error TS2445: Property 'd' is protected and only accessible within class 'D2' and its subclasses. |
| 14 | +tests/cases/compiler/protectedMembersThisParameter.ts(77,9): error TS2445: Property 'd2' is protected and only accessible within class 'D2' and its subclasses. |
| 15 | +tests/cases/compiler/protectedMembersThisParameter.ts(80,9): error TS2445: Property 'd' is protected and only accessible within class 'D2' and its subclasses. |
| 16 | +tests/cases/compiler/protectedMembersThisParameter.ts(81,9): error TS2445: Property 'd2' is protected and only accessible within class 'D2' and its subclasses. |
| 17 | + |
| 18 | + |
| 19 | +==== tests/cases/compiler/protectedMembersThisParameter.ts (16 errors) ==== |
| 20 | + class Message { |
| 21 | + protected secret(): void {} |
| 22 | + } |
| 23 | + class MessageWrapper { |
| 24 | + message: Message = new Message(); |
| 25 | + wrap<T>() { |
| 26 | + let m = this.message; |
| 27 | + let f = function(this: T) { |
| 28 | + m.secret(); // should error |
| 29 | + ~~~~~~ |
| 30 | +!!! error TS2445: Property 'secret' is protected and only accessible within class 'Message' and its subclasses. |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + class A { |
| 36 | + protected a() {} |
| 37 | + } |
| 38 | + class B extends A { |
| 39 | + protected b() {} |
| 40 | + } |
| 41 | + class C extends A { |
| 42 | + protected c() {} |
| 43 | + } |
| 44 | + class Z { |
| 45 | + protected z() {} |
| 46 | + } |
| 47 | + |
| 48 | + function bA<T extends A>(this: T, arg: B) { |
| 49 | + this.a(); |
| 50 | + arg.a(); |
| 51 | + arg.b(); // should error to avoid cross-hierarchy protected access https://www.typescriptlang.org/docs/handbook/2/classes.html#cross-hierarchy-protected-access |
| 52 | + ~ |
| 53 | +!!! error TS2445: Property 'b' is protected and only accessible within class 'B' and its subclasses. |
| 54 | + } |
| 55 | + function bB<T extends B>(this: T, arg: B) { |
| 56 | + this.a(); |
| 57 | + this.b(); |
| 58 | + arg.a(); |
| 59 | + arg.b(); |
| 60 | + } |
| 61 | + function bC<T extends C>(this: T, arg: B) { |
| 62 | + this.a(); |
| 63 | + this.c(); |
| 64 | + arg.a(); // should error |
| 65 | + ~ |
| 66 | +!!! error TS2446: Property 'a' is protected and only accessible through an instance of class 'C'. This is an instance of class 'B'. |
| 67 | + arg.b(); // should error |
| 68 | + ~ |
| 69 | +!!! error TS2445: Property 'b' is protected and only accessible within class 'B' and its subclasses. |
| 70 | + } |
| 71 | + function bZ<T extends Z>(this: T, arg: B) { |
| 72 | + this.z(); |
| 73 | + arg.a(); // should error |
| 74 | + ~ |
| 75 | +!!! error TS2445: Property 'a' is protected and only accessible within class 'A' and its subclasses. |
| 76 | + arg.b(); // should error |
| 77 | + ~ |
| 78 | +!!! error TS2445: Property 'b' is protected and only accessible within class 'B' and its subclasses. |
| 79 | + } |
| 80 | + function bString<T extends string>(this: T, arg: B) { |
| 81 | + this.toLowerCase(); |
| 82 | + arg.a(); // should error |
| 83 | + ~ |
| 84 | +!!! error TS2445: Property 'a' is protected and only accessible within class 'A' and its subclasses. |
| 85 | + arg.b(); // should error |
| 86 | + ~ |
| 87 | +!!! error TS2445: Property 'b' is protected and only accessible within class 'B' and its subclasses. |
| 88 | + } |
| 89 | + function bAny<T>(this: T, arg: B) { |
| 90 | + arg.a(); // should error |
| 91 | + ~ |
| 92 | +!!! error TS2445: Property 'a' is protected and only accessible within class 'A' and its subclasses. |
| 93 | + arg.b(); // should error |
| 94 | + ~ |
| 95 | +!!! error TS2445: Property 'b' is protected and only accessible within class 'B' and its subclasses. |
| 96 | + } |
| 97 | + |
| 98 | + class D { |
| 99 | + protected d() {} |
| 100 | + |
| 101 | + derived1(arg: D1) { |
| 102 | + arg.d(); |
| 103 | + arg.d1(); // should error |
| 104 | + ~~ |
| 105 | +!!! error TS2445: Property 'd1' is protected and only accessible within class 'D1' and its subclasses. |
| 106 | + } |
| 107 | + derived1ThisD(this: D, arg: D1) { |
| 108 | + arg.d(); |
| 109 | + arg.d1(); // should error |
| 110 | + ~~ |
| 111 | +!!! error TS2445: Property 'd1' is protected and only accessible within class 'D1' and its subclasses. |
| 112 | + } |
| 113 | + derived1ThisD1(this: D1, arg: D1) { |
| 114 | + arg.d(); |
| 115 | + arg.d1(); |
| 116 | + } |
| 117 | + |
| 118 | + derived2(arg: D2) { |
| 119 | + arg.d(); // should error because of overridden method in D2 |
| 120 | + ~ |
| 121 | +!!! error TS2445: Property 'd' is protected and only accessible within class 'D2' and its subclasses. |
| 122 | + arg.d2(); // should error |
| 123 | + ~~ |
| 124 | +!!! error TS2445: Property 'd2' is protected and only accessible within class 'D2' and its subclasses. |
| 125 | + } |
| 126 | + derived2ThisD(this: D, arg: D2) { |
| 127 | + arg.d(); // should error because of overridden method in D2 |
| 128 | + ~ |
| 129 | +!!! error TS2445: Property 'd' is protected and only accessible within class 'D2' and its subclasses. |
| 130 | + arg.d2(); // should error |
| 131 | + ~~ |
| 132 | +!!! error TS2445: Property 'd2' is protected and only accessible within class 'D2' and its subclasses. |
| 133 | + } |
| 134 | + derived2ThisD2(this: D2, arg: D2) { |
| 135 | + arg.d(); |
| 136 | + arg.d2(); |
| 137 | + } |
| 138 | + } |
| 139 | + class D1 extends D { |
| 140 | + protected d1() {} |
| 141 | + } |
| 142 | + class D2 extends D { |
| 143 | + protected d() {} |
| 144 | + protected d2() {} |
| 145 | + } |
| 146 | + |
| 147 | + |
0 commit comments