More tests for protected members

This commit is contained in:
Anders Hejlsberg 2014-09-19 14:01:07 -07:00
parent ddfe28db66
commit b5b0777ad5
28 changed files with 1283 additions and 20 deletions

View File

@ -1,8 +1,10 @@
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(6,5): error TS1089: 'private' modifier cannot appear on a constructor declaration.
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(18,9): error TS1089: 'private' modifier cannot appear on a constructor declaration.
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(10,5): error TS1089: 'protected' modifier cannot appear on a constructor declaration.
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(23,9): error TS1089: 'private' modifier cannot appear on a constructor declaration.
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(27,9): error TS1089: 'protected' modifier cannot appear on a constructor declaration.
==== tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts (2 errors) ====
==== tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts (4 errors) ====
class C {
public constructor(public x: number) { }
}
@ -13,8 +15,15 @@ tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessib
!!! error TS1089: 'private' modifier cannot appear on a constructor declaration.
}
class E {
protected constructor(public x: number) { } // error
~~~~~~~~~
!!! error TS1089: 'protected' modifier cannot appear on a constructor declaration.
}
var c = new C(1);
var d = new D(1);
var e = new E(1);
module Generic {
class C<T> {
@ -27,7 +36,14 @@ tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessib
!!! error TS1089: 'private' modifier cannot appear on a constructor declaration.
}
class E<T> {
protected constructor(public x: T) { } // error
~~~~~~~~~
!!! error TS1089: 'protected' modifier cannot appear on a constructor declaration.
}
var c = new C(1);
var d = new D(1);
var e = new E(1);
}

View File

@ -0,0 +1,62 @@
tests/cases/conformance/classes/members/accessibility/classPropertyAsProtected.ts(3,19): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
tests/cases/conformance/classes/members/accessibility/classPropertyAsProtected.ts(4,19): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
tests/cases/conformance/classes/members/accessibility/classPropertyAsProtected.ts(8,26): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
tests/cases/conformance/classes/members/accessibility/classPropertyAsProtected.ts(9,26): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
tests/cases/conformance/classes/members/accessibility/classPropertyAsProtected.ts(15,1): error TS2445: Property 'x' is protected and only accessible within class 'C' and its subclasses.
tests/cases/conformance/classes/members/accessibility/classPropertyAsProtected.ts(16,1): error TS2445: Property 'y' is protected and only accessible within class 'C' and its subclasses.
tests/cases/conformance/classes/members/accessibility/classPropertyAsProtected.ts(17,1): error TS2445: Property 'y' is protected and only accessible within class 'C' and its subclasses.
tests/cases/conformance/classes/members/accessibility/classPropertyAsProtected.ts(18,1): error TS2445: Property 'foo' is protected and only accessible within class 'C' and its subclasses.
tests/cases/conformance/classes/members/accessibility/classPropertyAsProtected.ts(20,1): error TS2445: Property 'a' is protected and only accessible within class 'C' and its subclasses.
tests/cases/conformance/classes/members/accessibility/classPropertyAsProtected.ts(21,1): error TS2445: Property 'b' is protected and only accessible within class 'C' and its subclasses.
tests/cases/conformance/classes/members/accessibility/classPropertyAsProtected.ts(22,1): error TS2445: Property 'b' is protected and only accessible within class 'C' and its subclasses.
tests/cases/conformance/classes/members/accessibility/classPropertyAsProtected.ts(23,1): error TS2445: Property 'foo' is protected and only accessible within class 'C' and its subclasses.
==== tests/cases/conformance/classes/members/accessibility/classPropertyAsProtected.ts (12 errors) ====
class C {
protected x: string;
protected get y() { return null; }
~
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
protected set y(x) { }
~
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
protected foo() { }
protected static a: string;
protected static get b() { return null; }
~
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
protected static set b(x) { }
~
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
protected static foo() { }
}
var c: C;
// all errors
c.x;
~~~
!!! error TS2445: Property 'x' is protected and only accessible within class 'C' and its subclasses.
c.y;
~~~
!!! error TS2445: Property 'y' is protected and only accessible within class 'C' and its subclasses.
c.y = 1;
~~~
!!! error TS2445: Property 'y' is protected and only accessible within class 'C' and its subclasses.
c.foo();
~~~~~
!!! error TS2445: Property 'foo' is protected and only accessible within class 'C' and its subclasses.
C.a;
~~~
!!! error TS2445: Property 'a' is protected and only accessible within class 'C' and its subclasses.
C.b();
~~~
!!! error TS2445: Property 'b' is protected and only accessible within class 'C' and its subclasses.
C.b = 1;
~~~
!!! error TS2445: Property 'b' is protected and only accessible within class 'C' and its subclasses.
C.foo();
~~~~~
!!! error TS2445: Property 'foo' is protected and only accessible within class 'C' and its subclasses.

View File

@ -1,12 +1,14 @@
tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties.ts(8,10): error TS2341: Property 'x' is private and only accessible within class 'C'.
tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties.ts(17,10): error TS2341: Property 'x' is private and only accessible within class 'D<T>'.
tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties.ts(18,12): error TS2339: Property 'a' does not exist on type 'D<string>'.
tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties.ts(9,10): error TS2445: Property 'z' is protected and only accessible within class 'C' and its subclasses.
tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties.ts(18,10): error TS2341: Property 'x' is private and only accessible within class 'D<T>'.
tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties.ts(19,12): error TS2339: Property 'a' does not exist on type 'D<string>'.
tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties.ts(20,10): error TS2445: Property 'z' is protected and only accessible within class 'D<T>' and its subclasses.
==== tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties.ts (3 errors) ====
==== tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties.ts (5 errors) ====
class C {
y: string;
constructor(private x: string) { }
constructor(private x: string, protected z: string) { }
}
var c: C;
@ -14,10 +16,13 @@ tests/cases/conformance/classes/constructorDeclarations/constructorParameters/co
var r2 = c.x; // error
~~~
!!! error TS2341: Property 'x' is private and only accessible within class 'C'.
var r3 = c.z; // error
~~~
!!! error TS2445: Property 'z' is protected and only accessible within class 'C' and its subclasses.
class D<T> {
y: T;
constructor(a: T, private x: T) { }
constructor(a: T, private x: T, protected z: T) { }
}
var d: D<string>;
@ -27,4 +32,8 @@ tests/cases/conformance/classes/constructorDeclarations/constructorParameters/co
!!! error TS2341: Property 'x' is private and only accessible within class 'D<T>'.
var r3 = d.a; // error
~
!!! error TS2339: Property 'a' does not exist on type 'D<string>'.
!!! error TS2339: Property 'a' does not exist on type 'D<string>'.
var r4 = d.z; // error
~~~
!!! error TS2445: Property 'z' is protected and only accessible within class 'D<T>' and its subclasses.

View File

@ -1,36 +1,42 @@
//// [constructorParameterProperties.ts]
class C {
y: string;
constructor(private x: string) { }
constructor(private x: string, protected z: string) { }
}
var c: C;
var r = c.y;
var r2 = c.x; // error
var r3 = c.z; // error
class D<T> {
y: T;
constructor(a: T, private x: T) { }
constructor(a: T, private x: T, protected z: T) { }
}
var d: D<string>;
var r = d.y;
var r2 = d.x; // error
var r3 = d.a; // error
var r3 = d.a; // error
var r4 = d.z; // error
//// [constructorParameterProperties.js]
var C = (function () {
function C(x) {
function C(x, z) {
this.x = x;
this.z = z;
}
return C;
})();
var c;
var r = c.y;
var r2 = c.x; // error
var r3 = c.z; // error
var D = (function () {
function D(a, x) {
function D(a, x, z) {
this.x = x;
this.z = z;
}
return D;
})();
@ -38,3 +44,4 @@ var d;
var r = d.y;
var r2 = d.x; // error
var r3 = d.a; // error
var r4 = d.z; // error

View File

@ -1,8 +1,9 @@
tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties2.ts(11,24): error TS2300: Duplicate identifier 'y'.
tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties2.ts(19,25): error TS2300: Duplicate identifier 'y'.
tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties2.ts(27,27): error TS2300: Duplicate identifier 'y'.
==== tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties2.ts (2 errors) ====
==== tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties2.ts (3 errors) ====
class C {
y: number;
constructor(y: number) { } // ok
@ -29,4 +30,15 @@ tests/cases/conformance/classes/constructorDeclarations/constructorParameters/co
}
var e: E;
var r3 = e.y; // error
var r3 = e.y; // error
class F {
y: number;
constructor(protected y: number) { } // error
~
!!! error TS2300: Duplicate identifier 'y'.
}
var f: F;
var r4 = f.y; // error

View File

@ -21,7 +21,16 @@ class E {
}
var e: E;
var r3 = e.y; // error
var r3 = e.y; // error
class F {
y: number;
constructor(protected y: number) { } // error
}
var f: F;
var r4 = f.y; // error
//// [constructorParameterProperties2.js]
var C = (function () {
@ -47,3 +56,11 @@ var E = (function () {
})();
var e;
var r3 = e.y; // error
var F = (function () {
function F(y) {
this.y = y;
} // error
return F;
})();
var f;
var r4 = f.y; // error

View File

@ -0,0 +1,74 @@
tests/cases/conformance/interfaces/interfacesExtendingClasses/implementingAnInterfaceExtendingClassWithProtecteds.ts(9,7): error TS2421: Class 'Bar' incorrectly implements interface 'I':
Property 'y' is missing in type 'Bar'.
tests/cases/conformance/interfaces/interfacesExtendingClasses/implementingAnInterfaceExtendingClassWithProtecteds.ts(12,7): error TS2421: Class 'Bar2' incorrectly implements interface 'I':
Property 'x' is missing in type 'Bar2'.
tests/cases/conformance/interfaces/interfacesExtendingClasses/implementingAnInterfaceExtendingClassWithProtecteds.ts(16,7): error TS2421: Class 'Bar3' incorrectly implements interface 'I':
Property 'x' is protected but type 'Bar3' is not a class derived from 'Foo'.
tests/cases/conformance/interfaces/interfacesExtendingClasses/implementingAnInterfaceExtendingClassWithProtecteds.ts(21,7): error TS2421: Class 'Bar4' incorrectly implements interface 'I':
Property 'x' is protected but type 'Bar4' is not a class derived from 'Foo'.
tests/cases/conformance/interfaces/interfacesExtendingClasses/implementingAnInterfaceExtendingClassWithProtecteds.ts(26,7): error TS2421: Class 'Bar5' incorrectly implements interface 'I':
Property 'y' is missing in type 'Bar5'.
tests/cases/conformance/interfaces/interfacesExtendingClasses/implementingAnInterfaceExtendingClassWithProtecteds.ts(29,7): error TS2421: Class 'Bar6' incorrectly implements interface 'I':
Property 'y' is protected in type 'Bar6' but public in type 'I'.
==== tests/cases/conformance/interfaces/interfacesExtendingClasses/implementingAnInterfaceExtendingClassWithProtecteds.ts (6 errors) ====
class Foo {
protected x: string;
}
interface I extends Foo {
y: number;
}
class Bar implements I { // error
~~~
!!! error TS2421: Class 'Bar' incorrectly implements interface 'I':
!!! error TS2421: Property 'y' is missing in type 'Bar'.
}
class Bar2 implements I { // error
~~~~
!!! error TS2421: Class 'Bar2' incorrectly implements interface 'I':
!!! error TS2421: Property 'x' is missing in type 'Bar2'.
y: number;
}
class Bar3 implements I { // error
~~~~
!!! error TS2421: Class 'Bar3' incorrectly implements interface 'I':
!!! error TS2421: Property 'x' is protected but type 'Bar3' is not a class derived from 'Foo'.
x: string;
y: number;
}
class Bar4 implements I { // error
~~~~
!!! error TS2421: Class 'Bar4' incorrectly implements interface 'I':
!!! error TS2421: Property 'x' is protected but type 'Bar4' is not a class derived from 'Foo'.
protected x: string;
y: number;
}
class Bar5 extends Foo implements I { // error
~~~~
!!! error TS2421: Class 'Bar5' incorrectly implements interface 'I':
!!! error TS2421: Property 'y' is missing in type 'Bar5'.
}
class Bar6 extends Foo implements I { // error
~~~~
!!! error TS2421: Class 'Bar6' incorrectly implements interface 'I':
!!! error TS2421: Property 'y' is protected in type 'Bar6' but public in type 'I'.
protected y: number;
}
class Bar7 extends Foo implements I {
y: number;
}
class Bar8 extends Foo implements I {
x: string;
y: number;
}

View File

@ -0,0 +1,103 @@
//// [implementingAnInterfaceExtendingClassWithProtecteds.ts]
class Foo {
protected x: string;
}
interface I extends Foo {
y: number;
}
class Bar implements I { // error
}
class Bar2 implements I { // error
y: number;
}
class Bar3 implements I { // error
x: string;
y: number;
}
class Bar4 implements I { // error
protected x: string;
y: number;
}
class Bar5 extends Foo implements I { // error
}
class Bar6 extends Foo implements I { // error
protected y: number;
}
class Bar7 extends Foo implements I {
y: number;
}
class Bar8 extends Foo implements I {
x: string;
y: number;
}
//// [implementingAnInterfaceExtendingClassWithProtecteds.js]
var __extends = this.__extends || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
var Foo = (function () {
function Foo() {
}
return Foo;
})();
var Bar = (function () {
function Bar() {
}
return Bar;
})();
var Bar2 = (function () {
function Bar2() {
}
return Bar2;
})();
var Bar3 = (function () {
function Bar3() {
}
return Bar3;
})();
var Bar4 = (function () {
function Bar4() {
}
return Bar4;
})();
var Bar5 = (function (_super) {
__extends(Bar5, _super);
function Bar5() {
_super.apply(this, arguments);
}
return Bar5;
})(Foo);
var Bar6 = (function (_super) {
__extends(Bar6, _super);
function Bar6() {
_super.apply(this, arguments);
}
return Bar6;
})(Foo);
var Bar7 = (function (_super) {
__extends(Bar7, _super);
function Bar7() {
_super.apply(this, arguments);
}
return Bar7;
})(Foo);
var Bar8 = (function (_super) {
__extends(Bar8, _super);
function Bar8() {
_super.apply(this, arguments);
}
return Bar8;
})(Foo);

View File

@ -0,0 +1,26 @@
tests/cases/conformance/interfaces/interfacesExtendingClasses/interfaceExtendingClassWithProtecteds.ts(5,11): error TS2429: Interface 'I' incorrectly extends interface 'Foo':
Property 'x' is protected but type 'I' is not a class derived from 'Foo'.
tests/cases/conformance/interfaces/interfacesExtendingClasses/interfaceExtendingClassWithProtecteds.ts(15,10): error TS2445: Property 'x' is protected and only accessible within class 'Foo' and its subclasses.
==== tests/cases/conformance/interfaces/interfacesExtendingClasses/interfaceExtendingClassWithProtecteds.ts (2 errors) ====
class Foo {
protected x: string;
}
interface I extends Foo { // error
~
!!! error TS2429: Interface 'I' incorrectly extends interface 'Foo':
!!! error TS2429: Property 'x' is protected but type 'I' is not a class derived from 'Foo'.
x: string;
}
interface I2 extends Foo {
y: string;
}
var i: I2;
var r = i.y;
var r2 = i.x; // error
~~~
!!! error TS2445: Property 'x' is protected and only accessible within class 'Foo' and its subclasses.

View File

@ -0,0 +1,26 @@
//// [interfaceExtendingClassWithProtecteds.ts]
class Foo {
protected x: string;
}
interface I extends Foo { // error
x: string;
}
interface I2 extends Foo {
y: string;
}
var i: I2;
var r = i.y;
var r2 = i.x; // error
//// [interfaceExtendingClassWithProtecteds.js]
var Foo = (function () {
function Foo() {
}
return Foo;
})();
var i;
var r = i.y;
var r2 = i.x; // error

View File

@ -0,0 +1,51 @@
tests/cases/conformance/interfaces/interfacesExtendingClasses/interfaceExtendingClassWithProtecteds2.ts(9,11): error TS2320: Interface 'I3' cannot simultaneously extend types 'Foo' and 'Bar':
Named properties 'x' of types 'Foo' and 'Bar' are not identical.
tests/cases/conformance/interfaces/interfacesExtendingClasses/interfaceExtendingClassWithProtecteds2.ts(12,11): error TS2429: Interface 'I4' incorrectly extends interface 'Bar':
Property 'x' is protected but type 'I4' is not a class derived from 'Bar'.
tests/cases/conformance/interfaces/interfacesExtendingClasses/interfaceExtendingClassWithProtecteds2.ts(12,11): error TS2429: Interface 'I4' incorrectly extends interface 'Foo':
Property 'x' is protected but type 'I4' is not a class derived from 'Foo'.
tests/cases/conformance/interfaces/interfacesExtendingClasses/interfaceExtendingClassWithProtecteds2.ts(26,10): error TS2445: Property 'x' is protected and only accessible within class 'Foo' and its subclasses.
tests/cases/conformance/interfaces/interfacesExtendingClasses/interfaceExtendingClassWithProtecteds2.ts(27,10): error TS2445: Property 'y' is protected and only accessible within class 'Baz' and its subclasses.
==== tests/cases/conformance/interfaces/interfacesExtendingClasses/interfaceExtendingClassWithProtecteds2.ts (5 errors) ====
class Foo {
protected x: string;
}
class Bar {
protected x: string;
}
interface I3 extends Foo, Bar { // error
~~
!!! error TS2320: Interface 'I3' cannot simultaneously extend types 'Foo' and 'Bar':
!!! error TS2320: Named properties 'x' of types 'Foo' and 'Bar' are not identical.
}
interface I4 extends Foo, Bar { // error
~~
!!! error TS2429: Interface 'I4' incorrectly extends interface 'Bar':
!!! error TS2429: Property 'x' is protected but type 'I4' is not a class derived from 'Bar'.
~~
!!! error TS2429: Interface 'I4' incorrectly extends interface 'Foo':
!!! error TS2429: Property 'x' is protected but type 'I4' is not a class derived from 'Foo'.
x: string;
}
class Baz {
protected y: string;
}
interface I5 extends Foo, Baz {
z: string;
}
var i: I5;
var r: string = i.z;
var r2 = i.x; // error
~~~
!!! error TS2445: Property 'x' is protected and only accessible within class 'Foo' and its subclasses.
var r3 = i.y; // error
~~~
!!! error TS2445: Property 'y' is protected and only accessible within class 'Baz' and its subclasses.

View File

@ -0,0 +1,49 @@
//// [interfaceExtendingClassWithProtecteds2.ts]
class Foo {
protected x: string;
}
class Bar {
protected x: string;
}
interface I3 extends Foo, Bar { // error
}
interface I4 extends Foo, Bar { // error
x: string;
}
class Baz {
protected y: string;
}
interface I5 extends Foo, Baz {
z: string;
}
var i: I5;
var r: string = i.z;
var r2 = i.x; // error
var r3 = i.y; // error
//// [interfaceExtendingClassWithProtecteds2.js]
var Foo = (function () {
function Foo() {
}
return Foo;
})();
var Bar = (function () {
function Bar() {
}
return Bar;
})();
var Baz = (function () {
function Baz() {
}
return Baz;
})();
var i;
var r = i.z;
var r2 = i.x; // error
var r3 = i.y; // error

View File

@ -0,0 +1,117 @@
//// [protectedClassPropertyAccessibleWithinClass.ts]
// no errors
class C {
protected x: string;
protected get y() { return this.x; }
protected set y(x) { this.y = this.x; }
protected foo() { return this.foo; }
protected static x: string;
protected static get y() { return this.x; }
protected static set y(x) { this.y = this.x; }
protected static foo() { return this.foo; }
protected static bar() { this.foo(); }
}
// added level of function nesting
class C2 {
protected x: string;
protected get y() { () => this.x; return null; }
protected set y(x) { () => { this.y = this.x; } }
protected foo() { () => this.foo; }
protected static x: string;
protected static get y() { () => this.x; return null; }
protected static set y(x) {
() => { this.y = this.x; }
}
protected static foo() { () => this.foo; }
protected static bar() { () => this.foo(); }
}
//// [protectedClassPropertyAccessibleWithinClass.js]
// no errors
var C = (function () {
function C() {
}
Object.defineProperty(C.prototype, "y", {
get: function () {
return this.x;
},
set: function (x) {
this.y = this.x;
},
enumerable: true,
configurable: true
});
C.prototype.foo = function () {
return this.foo;
};
Object.defineProperty(C, "y", {
get: function () {
return this.x;
},
set: function (x) {
this.y = this.x;
},
enumerable: true,
configurable: true
});
C.foo = function () {
return this.foo;
};
C.bar = function () {
this.foo();
};
return C;
})();
// added level of function nesting
var C2 = (function () {
function C2() {
}
Object.defineProperty(C2.prototype, "y", {
get: function () {
var _this = this;
(function () { return _this.x; });
return null;
},
set: function (x) {
var _this = this;
(function () {
_this.y = _this.x;
});
},
enumerable: true,
configurable: true
});
C2.prototype.foo = function () {
var _this = this;
(function () { return _this.foo; });
};
Object.defineProperty(C2, "y", {
get: function () {
var _this = this;
(function () { return _this.x; });
return null;
},
set: function (x) {
var _this = this;
(function () {
_this.y = _this.x;
});
},
enumerable: true,
configurable: true
});
C2.foo = function () {
var _this = this;
(function () { return _this.foo; });
};
C2.bar = function () {
var _this = this;
(function () { return _this.foo(); });
};
return C2;
})();

View File

@ -0,0 +1,139 @@
=== tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinClass.ts ===
// no errors
class C {
>C : C
protected x: string;
>x : string
protected get y() { return this.x; }
>y : string
>this.x : string
>this : C
>x : string
protected set y(x) { this.y = this.x; }
>y : string
>x : string
>this.y = this.x : string
>this.y : string
>this : C
>y : string
>this.x : string
>this : C
>x : string
protected foo() { return this.foo; }
>foo : () => any
>this.foo : () => any
>this : C
>foo : () => any
protected static x: string;
>x : string
protected static get y() { return this.x; }
>y : string
>this.x : string
>this : typeof C
>x : string
protected static set y(x) { this.y = this.x; }
>y : string
>x : string
>this.y = this.x : string
>this.y : string
>this : typeof C
>y : string
>this.x : string
>this : typeof C
>x : string
protected static foo() { return this.foo; }
>foo : () => typeof C.foo
>this.foo : () => typeof C.foo
>this : typeof C
>foo : () => typeof C.foo
protected static bar() { this.foo(); }
>bar : () => void
>this.foo() : () => typeof C.foo
>this.foo : () => typeof C.foo
>this : typeof C
>foo : () => typeof C.foo
}
// added level of function nesting
class C2 {
>C2 : C2
protected x: string;
>x : string
protected get y() { () => this.x; return null; }
>y : any
>() => this.x : () => string
>this.x : string
>this : C2
>x : string
protected set y(x) { () => { this.y = this.x; } }
>y : any
>x : any
>() => { this.y = this.x; } : () => void
>this.y = this.x : string
>this.y : any
>this : C2
>y : any
>this.x : string
>this : C2
>x : string
protected foo() { () => this.foo; }
>foo : () => void
>() => this.foo : () => () => void
>this.foo : () => void
>this : C2
>foo : () => void
protected static x: string;
>x : string
protected static get y() { () => this.x; return null; }
>y : any
>() => this.x : () => string
>this.x : string
>this : typeof C2
>x : string
protected static set y(x) {
>y : any
>x : any
() => { this.y = this.x; }
>() => { this.y = this.x; } : () => void
>this.y = this.x : string
>this.y : any
>this : typeof C2
>y : any
>this.x : string
>this : typeof C2
>x : string
}
protected static foo() { () => this.foo; }
>foo : () => void
>() => this.foo : () => () => void
>this.foo : () => void
>this : typeof C2
>foo : () => void
protected static bar() { () => this.foo(); }
>bar : () => void
>() => this.foo() : () => void
>this.foo() : void
>this.foo : () => void
>this : typeof C2
>foo : () => void
}

View File

@ -0,0 +1,73 @@
//// [protectedClassPropertyAccessibleWithinSubclass.ts]
// no errors
class B {
protected x: string;
protected static x: string;
}
class C extends B {
protected get y() { return this.x; }
protected set y(x) { this.y = this.x; }
protected foo() { return this.x; }
protected bar() { return this.foo(); }
protected static get y() { return this.x; }
protected static set y(x) { this.y = this.x; }
protected static foo() { return this.x; }
protected static bar() { this.foo(); }
}
//// [protectedClassPropertyAccessibleWithinSubclass.js]
// no errors
var __extends = this.__extends || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
var B = (function () {
function B() {
}
return B;
})();
var C = (function (_super) {
__extends(C, _super);
function C() {
_super.apply(this, arguments);
}
Object.defineProperty(C.prototype, "y", {
get: function () {
return this.x;
},
set: function (x) {
this.y = this.x;
},
enumerable: true,
configurable: true
});
C.prototype.foo = function () {
return this.x;
};
C.prototype.bar = function () {
return this.foo();
};
Object.defineProperty(C, "y", {
get: function () {
return this.x;
},
set: function (x) {
this.y = this.x;
},
enumerable: true,
configurable: true
});
C.foo = function () {
return this.x;
};
C.bar = function () {
this.foo();
};
return C;
})(B);

View File

@ -0,0 +1,78 @@
=== tests/cases/conformance/classes/members/accessibility/protectedClassPropertyAccessibleWithinSubclass.ts ===
// no errors
class B {
>B : B
protected x: string;
>x : string
protected static x: string;
>x : string
}
class C extends B {
>C : C
>B : B
protected get y() { return this.x; }
>y : string
>this.x : string
>this : C
>x : string
protected set y(x) { this.y = this.x; }
>y : string
>x : string
>this.y = this.x : string
>this.y : string
>this : C
>y : string
>this.x : string
>this : C
>x : string
protected foo() { return this.x; }
>foo : () => string
>this.x : string
>this : C
>x : string
protected bar() { return this.foo(); }
>bar : () => string
>this.foo() : string
>this.foo : () => string
>this : C
>foo : () => string
protected static get y() { return this.x; }
>y : string
>this.x : string
>this : typeof C
>x : string
protected static set y(x) { this.y = this.x; }
>y : string
>x : string
>this.y = this.x : string
>this.y : string
>this : typeof C
>y : string
>this.x : string
>this : typeof C
>x : string
protected static foo() { return this.x; }
>foo : () => string
>this.x : string
>this : typeof C
>x : string
protected static bar() { this.foo(); }
>bar : () => void
>this.foo() : string
>this.foo : () => string
>this : typeof C
>foo : () => string
}

View File

@ -0,0 +1,86 @@
tests/cases/conformance/classes/members/accessibility/protectedInstanceMemberAccessibility.ts(14,23): error TS2339: Property 'z' does not exist on type 'B'.
tests/cases/conformance/classes/members/accessibility/protectedInstanceMemberAccessibility.ts(16,24): error TS2340: Only public and protected methods of the base class are accessible via the 'super' keyword
tests/cases/conformance/classes/members/accessibility/protectedInstanceMemberAccessibility.ts(18,24): error TS2339: Property 'y' does not exist on type 'A'.
tests/cases/conformance/classes/members/accessibility/protectedInstanceMemberAccessibility.ts(19,24): error TS2339: Property 'z' does not exist on type 'A'.
tests/cases/conformance/classes/members/accessibility/protectedInstanceMemberAccessibility.ts(22,18): error TS2446: Property 'x' is protected and only accessible through an instance of class 'B'.
tests/cases/conformance/classes/members/accessibility/protectedInstanceMemberAccessibility.ts(23,18): error TS2446: Property 'f' is protected and only accessible through an instance of class 'B'.
tests/cases/conformance/classes/members/accessibility/protectedInstanceMemberAccessibility.ts(24,20): error TS2339: Property 'y' does not exist on type 'A'.
tests/cases/conformance/classes/members/accessibility/protectedInstanceMemberAccessibility.ts(25,20): error TS2339: Property 'z' does not exist on type 'A'.
tests/cases/conformance/classes/members/accessibility/protectedInstanceMemberAccessibility.ts(31,20): error TS2339: Property 'z' does not exist on type 'B'.
tests/cases/conformance/classes/members/accessibility/protectedInstanceMemberAccessibility.ts(34,18): error TS2446: Property 'x' is protected and only accessible through an instance of class 'B'.
tests/cases/conformance/classes/members/accessibility/protectedInstanceMemberAccessibility.ts(35,18): error TS2446: Property 'f' is protected and only accessible through an instance of class 'B'.
tests/cases/conformance/classes/members/accessibility/protectedInstanceMemberAccessibility.ts(36,20): error TS2339: Property 'y' does not exist on type 'C'.
tests/cases/conformance/classes/members/accessibility/protectedInstanceMemberAccessibility.ts(37,18): error TS2445: Property 'z' is protected and only accessible within class 'C' and its subclasses.
==== tests/cases/conformance/classes/members/accessibility/protectedInstanceMemberAccessibility.ts (13 errors) ====
class A {
protected x: string;
protected f(): string {
return "hello";
}
}
class B extends A {
protected y: string;
g() {
var t1 = this.x;
var t2 = this.f();
var t3 = this.y;
var t4 = this.z; // error
~
!!! error TS2339: Property 'z' does not exist on type 'B'.
var s1 = super.x; // error
~
!!! error TS2340: Only public and protected methods of the base class are accessible via the 'super' keyword
var s2 = super.f();
var s3 = super.y; // error
~
!!! error TS2339: Property 'y' does not exist on type 'A'.
var s4 = super.z; // error
~
!!! error TS2339: Property 'z' does not exist on type 'A'.
var a: A;
var a1 = a.x; // error
~~~
!!! error TS2446: Property 'x' is protected and only accessible through an instance of class 'B'.
var a2 = a.f(); // error
~~~
!!! error TS2446: Property 'f' is protected and only accessible through an instance of class 'B'.
var a3 = a.y; // error
~
!!! error TS2339: Property 'y' does not exist on type 'A'.
var a4 = a.z; // error
~
!!! error TS2339: Property 'z' does not exist on type 'A'.
var b: B;
var b1 = b.x;
var b2 = b.f();
var b3 = b.y;
var b4 = b.z; // error
~
!!! error TS2339: Property 'z' does not exist on type 'B'.
var c: C;
var c1 = c.x; // error
~~~
!!! error TS2446: Property 'x' is protected and only accessible through an instance of class 'B'.
var c2 = c.f(); // error
~~~
!!! error TS2446: Property 'f' is protected and only accessible through an instance of class 'B'.
var c3 = c.y; // error
~
!!! error TS2339: Property 'y' does not exist on type 'C'.
var c4 = c.z; // error
~~~
!!! error TS2445: Property 'z' is protected and only accessible within class 'C' and its subclasses.
}
}
class C extends A {
protected z: string;
}

View File

@ -0,0 +1,100 @@
//// [protectedInstanceMemberAccessibility.ts]
class A {
protected x: string;
protected f(): string {
return "hello";
}
}
class B extends A {
protected y: string;
g() {
var t1 = this.x;
var t2 = this.f();
var t3 = this.y;
var t4 = this.z; // error
var s1 = super.x; // error
var s2 = super.f();
var s3 = super.y; // error
var s4 = super.z; // error
var a: A;
var a1 = a.x; // error
var a2 = a.f(); // error
var a3 = a.y; // error
var a4 = a.z; // error
var b: B;
var b1 = b.x;
var b2 = b.f();
var b3 = b.y;
var b4 = b.z; // error
var c: C;
var c1 = c.x; // error
var c2 = c.f(); // error
var c3 = c.y; // error
var c4 = c.z; // error
}
}
class C extends A {
protected z: string;
}
//// [protectedInstanceMemberAccessibility.js]
var __extends = this.__extends || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
var A = (function () {
function A() {
}
A.prototype.f = function () {
return "hello";
};
return A;
})();
var B = (function (_super) {
__extends(B, _super);
function B() {
_super.apply(this, arguments);
}
B.prototype.g = function () {
var t1 = this.x;
var t2 = this.f();
var t3 = this.y;
var t4 = this.z; // error
var s1 = _super.prototype.x; // error
var s2 = _super.prototype.f.call(this);
var s3 = _super.prototype.y; // error
var s4 = _super.prototype.z; // error
var a;
var a1 = a.x; // error
var a2 = a.f(); // error
var a3 = a.y; // error
var a4 = a.z; // error
var b;
var b1 = b.x;
var b2 = b.f();
var b3 = b.y;
var b4 = b.z; // error
var c;
var c1 = c.x; // error
var c2 = c.f(); // error
var c3 = c.y; // error
var c4 = c.z; // error
};
return B;
})(A);
var C = (function (_super) {
__extends(C, _super);
function C() {
_super.apply(this, arguments);
}
return C;
})(A);

View File

@ -6,8 +6,13 @@ class D {
private constructor(public x: number) { } // error
}
class E {
protected constructor(public x: number) { } // error
}
var c = new C(1);
var d = new D(1);
var e = new E(1);
module Generic {
class C<T> {
@ -18,6 +23,11 @@ module Generic {
private constructor(public x: T) { } // error
}
class E<T> {
protected constructor(public x: T) { } // error
}
var c = new C(1);
var d = new D(1);
var e = new E(1);
}

View File

@ -1,18 +1,20 @@
class C {
y: string;
constructor(private x: string) { }
constructor(private x: string, protected z: string) { }
}
var c: C;
var r = c.y;
var r2 = c.x; // error
var r3 = c.z; // error
class D<T> {
y: T;
constructor(a: T, private x: T) { }
constructor(a: T, private x: T, protected z: T) { }
}
var d: D<string>;
var r = d.y;
var r2 = d.x; // error
var r3 = d.a; // error
var r3 = d.a; // error
var r4 = d.z; // error

View File

@ -20,4 +20,12 @@ class E {
}
var e: E;
var r3 = e.y; // error
var r3 = e.y; // error
class F {
y: number;
constructor(protected y: number) { } // error
}
var f: F;
var r4 = f.y; // error

View File

@ -0,0 +1,23 @@
class C {
protected x: string;
protected get y() { return null; }
protected set y(x) { }
protected foo() { }
protected static a: string;
protected static get b() { return null; }
protected static set b(x) { }
protected static foo() { }
}
var c: C;
// all errors
c.x;
c.y;
c.y = 1;
c.foo();
C.a;
C.b();
C.b = 1;
C.foo();

View File

@ -0,0 +1,31 @@
// @target: ES5
// no errors
class C {
protected x: string;
protected get y() { return this.x; }
protected set y(x) { this.y = this.x; }
protected foo() { return this.foo; }
protected static x: string;
protected static get y() { return this.x; }
protected static set y(x) { this.y = this.x; }
protected static foo() { return this.foo; }
protected static bar() { this.foo(); }
}
// added level of function nesting
class C2 {
protected x: string;
protected get y() { () => this.x; return null; }
protected set y(x) { () => { this.y = this.x; } }
protected foo() { () => this.foo; }
protected static x: string;
protected static get y() { () => this.x; return null; }
protected static set y(x) {
() => { this.y = this.x; }
}
protected static foo() { () => this.foo; }
protected static bar() { () => this.foo(); }
}

View File

@ -0,0 +1,19 @@
// @target: ES5
// no errors
class B {
protected x: string;
protected static x: string;
}
class C extends B {
protected get y() { return this.x; }
protected set y(x) { this.y = this.x; }
protected foo() { return this.x; }
protected bar() { return this.foo(); }
protected static get y() { return this.x; }
protected static set y(x) { this.y = this.x; }
protected static foo() { return this.x; }
protected static bar() { this.foo(); }
}

View File

@ -0,0 +1,43 @@
class A {
protected x: string;
protected f(): string {
return "hello";
}
}
class B extends A {
protected y: string;
g() {
var t1 = this.x;
var t2 = this.f();
var t3 = this.y;
var t4 = this.z; // error
var s1 = super.x; // error
var s2 = super.f();
var s3 = super.y; // error
var s4 = super.z; // error
var a: A;
var a1 = a.x; // error
var a2 = a.f(); // error
var a3 = a.y; // error
var a4 = a.z; // error
var b: B;
var b1 = b.x;
var b2 = b.f();
var b3 = b.y;
var b4 = b.z; // error
var c: C;
var c1 = c.x; // error
var c2 = c.f(); // error
var c3 = c.y; // error
var c4 = c.z; // error
}
}
class C extends A {
protected z: string;
}

View File

@ -0,0 +1,40 @@
class Foo {
protected x: string;
}
interface I extends Foo {
y: number;
}
class Bar implements I { // error
}
class Bar2 implements I { // error
y: number;
}
class Bar3 implements I { // error
x: string;
y: number;
}
class Bar4 implements I { // error
protected x: string;
y: number;
}
class Bar5 extends Foo implements I { // error
}
class Bar6 extends Foo implements I { // error
protected y: number;
}
class Bar7 extends Foo implements I {
y: number;
}
class Bar8 extends Foo implements I {
x: string;
y: number;
}

View File

@ -0,0 +1,15 @@
class Foo {
protected x: string;
}
interface I extends Foo { // error
x: string;
}
interface I2 extends Foo {
y: string;
}
var i: I2;
var r = i.y;
var r2 = i.x; // error

View File

@ -0,0 +1,27 @@
class Foo {
protected x: string;
}
class Bar {
protected x: string;
}
interface I3 extends Foo, Bar { // error
}
interface I4 extends Foo, Bar { // error
x: string;
}
class Baz {
protected y: string;
}
interface I5 extends Foo, Baz {
z: string;
}
var i: I5;
var r: string = i.z;
var r2 = i.x; // error
var r3 = i.y; // error