From abd2a8526d67353aacd93be21da0fdf797906fae Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sat, 26 Sep 2015 12:58:53 -0700 Subject: [PATCH] Adding tests --- tests/baselines/reference/fluentClasses.js | 56 +++++++ .../baselines/reference/fluentClasses.symbols | 47 ++++++ tests/baselines/reference/fluentClasses.types | 50 ++++++ tests/baselines/reference/fluentInterfaces.js | 17 ++ .../reference/fluentInterfaces.symbols | 35 ++++ .../reference/fluentInterfaces.types | 38 +++++ .../reference/thisTypeErrors.errors.txt | 149 +++++++++++++++++ tests/baselines/reference/thisTypeErrors.js | 104 ++++++++++++ .../baselines/reference/thisTypeInClasses.js | 91 +++++++++++ .../reference/thisTypeInClasses.symbols | 140 ++++++++++++++++ .../reference/thisTypeInClasses.types | 150 ++++++++++++++++++ .../reference/thisTypeInInterfaces.js | 32 ++++ .../reference/thisTypeInInterfaces.symbols | 79 +++++++++ .../reference/thisTypeInInterfaces.types | 79 +++++++++ .../reference/typeRelationships.errors.txt | 54 +++++++ .../baselines/reference/typeRelationships.js | 94 +++++++++++ .../types/thisType/fluentClasses.ts | 17 ++ .../types/thisType/fluentInterfaces.ts | 11 ++ .../types/thisType/thisTypeErrors.ts | 55 +++++++ .../types/thisType/thisTypeInClasses.ts | 50 ++++++ .../types/thisType/thisTypeInInterfaces.ts | 28 ++++ .../types/thisType/typeRelationships.ts | 39 +++++ 22 files changed, 1415 insertions(+) create mode 100644 tests/baselines/reference/fluentClasses.js create mode 100644 tests/baselines/reference/fluentClasses.symbols create mode 100644 tests/baselines/reference/fluentClasses.types create mode 100644 tests/baselines/reference/fluentInterfaces.js create mode 100644 tests/baselines/reference/fluentInterfaces.symbols create mode 100644 tests/baselines/reference/fluentInterfaces.types create mode 100644 tests/baselines/reference/thisTypeErrors.errors.txt create mode 100644 tests/baselines/reference/thisTypeErrors.js create mode 100644 tests/baselines/reference/thisTypeInClasses.js create mode 100644 tests/baselines/reference/thisTypeInClasses.symbols create mode 100644 tests/baselines/reference/thisTypeInClasses.types create mode 100644 tests/baselines/reference/thisTypeInInterfaces.js create mode 100644 tests/baselines/reference/thisTypeInInterfaces.symbols create mode 100644 tests/baselines/reference/thisTypeInInterfaces.types create mode 100644 tests/baselines/reference/typeRelationships.errors.txt create mode 100644 tests/baselines/reference/typeRelationships.js create mode 100644 tests/cases/conformance/types/thisType/fluentClasses.ts create mode 100644 tests/cases/conformance/types/thisType/fluentInterfaces.ts create mode 100644 tests/cases/conformance/types/thisType/thisTypeErrors.ts create mode 100644 tests/cases/conformance/types/thisType/thisTypeInClasses.ts create mode 100644 tests/cases/conformance/types/thisType/thisTypeInInterfaces.ts create mode 100644 tests/cases/conformance/types/thisType/typeRelationships.ts diff --git a/tests/baselines/reference/fluentClasses.js b/tests/baselines/reference/fluentClasses.js new file mode 100644 index 00000000000..12f3cfff48d --- /dev/null +++ b/tests/baselines/reference/fluentClasses.js @@ -0,0 +1,56 @@ +//// [fluentClasses.ts] +class A { + foo() { + return this; + } +} +class B extends A { + bar() { + return this; + } +} +class C extends B { + baz() { + return this; + } +} +var c: C; +var z = c.foo().bar().baz(); // Fluent pattern + + +//// [fluentClasses.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var A = (function () { + function A() { + } + A.prototype.foo = function () { + return this; + }; + return A; +})(); +var B = (function (_super) { + __extends(B, _super); + function B() { + _super.apply(this, arguments); + } + B.prototype.bar = function () { + return this; + }; + return B; +})(A); +var C = (function (_super) { + __extends(C, _super); + function C() { + _super.apply(this, arguments); + } + C.prototype.baz = function () { + return this; + }; + return C; +})(B); +var c; +var z = c.foo().bar().baz(); // Fluent pattern diff --git a/tests/baselines/reference/fluentClasses.symbols b/tests/baselines/reference/fluentClasses.symbols new file mode 100644 index 00000000000..3125235b6b1 --- /dev/null +++ b/tests/baselines/reference/fluentClasses.symbols @@ -0,0 +1,47 @@ +=== tests/cases/conformance/types/thisType/fluentClasses.ts === +class A { +>A : Symbol(A, Decl(fluentClasses.ts, 0, 0)) + + foo() { +>foo : Symbol(foo, Decl(fluentClasses.ts, 0, 9)) + + return this; +>this : Symbol(A, Decl(fluentClasses.ts, 0, 0)) + } +} +class B extends A { +>B : Symbol(B, Decl(fluentClasses.ts, 4, 1)) +>A : Symbol(A, Decl(fluentClasses.ts, 0, 0)) + + bar() { +>bar : Symbol(bar, Decl(fluentClasses.ts, 5, 19)) + + return this; +>this : Symbol(B, Decl(fluentClasses.ts, 4, 1)) + } +} +class C extends B { +>C : Symbol(C, Decl(fluentClasses.ts, 9, 1)) +>B : Symbol(B, Decl(fluentClasses.ts, 4, 1)) + + baz() { +>baz : Symbol(baz, Decl(fluentClasses.ts, 10, 19)) + + return this; +>this : Symbol(C, Decl(fluentClasses.ts, 9, 1)) + } +} +var c: C; +>c : Symbol(c, Decl(fluentClasses.ts, 15, 3)) +>C : Symbol(C, Decl(fluentClasses.ts, 9, 1)) + +var z = c.foo().bar().baz(); // Fluent pattern +>z : Symbol(z, Decl(fluentClasses.ts, 16, 3)) +>c.foo().bar().baz : Symbol(C.baz, Decl(fluentClasses.ts, 10, 19)) +>c.foo().bar : Symbol(B.bar, Decl(fluentClasses.ts, 5, 19)) +>c.foo : Symbol(A.foo, Decl(fluentClasses.ts, 0, 9)) +>c : Symbol(c, Decl(fluentClasses.ts, 15, 3)) +>foo : Symbol(A.foo, Decl(fluentClasses.ts, 0, 9)) +>bar : Symbol(B.bar, Decl(fluentClasses.ts, 5, 19)) +>baz : Symbol(C.baz, Decl(fluentClasses.ts, 10, 19)) + diff --git a/tests/baselines/reference/fluentClasses.types b/tests/baselines/reference/fluentClasses.types new file mode 100644 index 00000000000..31ed6aafa01 --- /dev/null +++ b/tests/baselines/reference/fluentClasses.types @@ -0,0 +1,50 @@ +=== tests/cases/conformance/types/thisType/fluentClasses.ts === +class A { +>A : A + + foo() { +>foo : () => this + + return this; +>this : this + } +} +class B extends A { +>B : B +>A : A + + bar() { +>bar : () => this + + return this; +>this : this + } +} +class C extends B { +>C : C +>B : B + + baz() { +>baz : () => this + + return this; +>this : this + } +} +var c: C; +>c : C +>C : C + +var z = c.foo().bar().baz(); // Fluent pattern +>z : C +>c.foo().bar().baz() : C +>c.foo().bar().baz : () => C +>c.foo().bar() : C +>c.foo().bar : () => C +>c.foo() : C +>c.foo : () => C +>c : C +>foo : () => C +>bar : () => C +>baz : () => C + diff --git a/tests/baselines/reference/fluentInterfaces.js b/tests/baselines/reference/fluentInterfaces.js new file mode 100644 index 00000000000..1be15923d6c --- /dev/null +++ b/tests/baselines/reference/fluentInterfaces.js @@ -0,0 +1,17 @@ +//// [fluentInterfaces.ts] +interface A { + foo(): this; +} +interface B extends A { + bar(): this; +} +interface C extends B { + baz(): this; +} +var c: C; +var z = c.foo().bar().baz(); // Fluent pattern + + +//// [fluentInterfaces.js] +var c; +var z = c.foo().bar().baz(); // Fluent pattern diff --git a/tests/baselines/reference/fluentInterfaces.symbols b/tests/baselines/reference/fluentInterfaces.symbols new file mode 100644 index 00000000000..e059cdc127e --- /dev/null +++ b/tests/baselines/reference/fluentInterfaces.symbols @@ -0,0 +1,35 @@ +=== tests/cases/conformance/types/thisType/fluentInterfaces.ts === +interface A { +>A : Symbol(A, Decl(fluentInterfaces.ts, 0, 0)) + + foo(): this; +>foo : Symbol(foo, Decl(fluentInterfaces.ts, 0, 13)) +} +interface B extends A { +>B : Symbol(B, Decl(fluentInterfaces.ts, 2, 1)) +>A : Symbol(A, Decl(fluentInterfaces.ts, 0, 0)) + + bar(): this; +>bar : Symbol(bar, Decl(fluentInterfaces.ts, 3, 23)) +} +interface C extends B { +>C : Symbol(C, Decl(fluentInterfaces.ts, 5, 1)) +>B : Symbol(B, Decl(fluentInterfaces.ts, 2, 1)) + + baz(): this; +>baz : Symbol(baz, Decl(fluentInterfaces.ts, 6, 23)) +} +var c: C; +>c : Symbol(c, Decl(fluentInterfaces.ts, 9, 3)) +>C : Symbol(C, Decl(fluentInterfaces.ts, 5, 1)) + +var z = c.foo().bar().baz(); // Fluent pattern +>z : Symbol(z, Decl(fluentInterfaces.ts, 10, 3)) +>c.foo().bar().baz : Symbol(C.baz, Decl(fluentInterfaces.ts, 6, 23)) +>c.foo().bar : Symbol(B.bar, Decl(fluentInterfaces.ts, 3, 23)) +>c.foo : Symbol(A.foo, Decl(fluentInterfaces.ts, 0, 13)) +>c : Symbol(c, Decl(fluentInterfaces.ts, 9, 3)) +>foo : Symbol(A.foo, Decl(fluentInterfaces.ts, 0, 13)) +>bar : Symbol(B.bar, Decl(fluentInterfaces.ts, 3, 23)) +>baz : Symbol(C.baz, Decl(fluentInterfaces.ts, 6, 23)) + diff --git a/tests/baselines/reference/fluentInterfaces.types b/tests/baselines/reference/fluentInterfaces.types new file mode 100644 index 00000000000..26e5a74c188 --- /dev/null +++ b/tests/baselines/reference/fluentInterfaces.types @@ -0,0 +1,38 @@ +=== tests/cases/conformance/types/thisType/fluentInterfaces.ts === +interface A { +>A : A + + foo(): this; +>foo : () => this +} +interface B extends A { +>B : B +>A : A + + bar(): this; +>bar : () => this +} +interface C extends B { +>C : C +>B : B + + baz(): this; +>baz : () => this +} +var c: C; +>c : C +>C : C + +var z = c.foo().bar().baz(); // Fluent pattern +>z : C +>c.foo().bar().baz() : C +>c.foo().bar().baz : () => C +>c.foo().bar() : C +>c.foo().bar : () => C +>c.foo() : C +>c.foo : () => C +>c : C +>foo : () => C +>bar : () => C +>baz : () => C + diff --git a/tests/baselines/reference/thisTypeErrors.errors.txt b/tests/baselines/reference/thisTypeErrors.errors.txt new file mode 100644 index 00000000000..b21c96968a3 --- /dev/null +++ b/tests/baselines/reference/thisTypeErrors.errors.txt @@ -0,0 +1,149 @@ +tests/cases/conformance/types/thisType/thisTypeErrors.ts(1,9): error TS2526: 'this' type is available only in a non-static member of a class or interface. +tests/cases/conformance/types/thisType/thisTypeErrors.ts(2,14): error TS2526: 'this' type is available only in a non-static member of a class or interface. +tests/cases/conformance/types/thisType/thisTypeErrors.ts(3,9): error TS2526: 'this' type is available only in a non-static member of a class or interface. +tests/cases/conformance/types/thisType/thisTypeErrors.ts(5,16): error TS2526: 'this' type is available only in a non-static member of a class or interface. +tests/cases/conformance/types/thisType/thisTypeErrors.ts(5,23): error TS2526: 'this' type is available only in a non-static member of a class or interface. +tests/cases/conformance/types/thisType/thisTypeErrors.ts(6,12): error TS2526: 'this' type is available only in a non-static member of a class or interface. +tests/cases/conformance/types/thisType/thisTypeErrors.ts(11,13): error TS2526: 'this' type is available only in a non-static member of a class or interface. +tests/cases/conformance/types/thisType/thisTypeErrors.ts(12,14): error TS2526: 'this' type is available only in a non-static member of a class or interface. +tests/cases/conformance/types/thisType/thisTypeErrors.ts(13,18): error TS2526: 'this' type is available only in a non-static member of a class or interface. +tests/cases/conformance/types/thisType/thisTypeErrors.ts(14,23): error TS2526: 'this' type is available only in a non-static member of a class or interface. +tests/cases/conformance/types/thisType/thisTypeErrors.ts(15,15): error TS2526: 'this' type is available only in a non-static member of a class or interface. +tests/cases/conformance/types/thisType/thisTypeErrors.ts(15,22): error TS2526: 'this' type is available only in a non-static member of a class or interface. +tests/cases/conformance/types/thisType/thisTypeErrors.ts(19,13): error TS2526: 'this' type is available only in a non-static member of a class or interface. +tests/cases/conformance/types/thisType/thisTypeErrors.ts(20,14): error TS2526: 'this' type is available only in a non-static member of a class or interface. +tests/cases/conformance/types/thisType/thisTypeErrors.ts(21,18): error TS2526: 'this' type is available only in a non-static member of a class or interface. +tests/cases/conformance/types/thisType/thisTypeErrors.ts(22,23): error TS2526: 'this' type is available only in a non-static member of a class or interface. +tests/cases/conformance/types/thisType/thisTypeErrors.ts(23,15): error TS2526: 'this' type is available only in a non-static member of a class or interface. +tests/cases/conformance/types/thisType/thisTypeErrors.ts(23,22): error TS2526: 'this' type is available only in a non-static member of a class or interface. +tests/cases/conformance/types/thisType/thisTypeErrors.ts(27,15): error TS2526: 'this' type is available only in a non-static member of a class or interface. +tests/cases/conformance/types/thisType/thisTypeErrors.ts(28,17): error TS2526: 'this' type is available only in a non-static member of a class or interface. +tests/cases/conformance/types/thisType/thisTypeErrors.ts(29,19): error TS2526: 'this' type is available only in a non-static member of a class or interface. +tests/cases/conformance/types/thisType/thisTypeErrors.ts(29,26): error TS2526: 'this' type is available only in a non-static member of a class or interface. +tests/cases/conformance/types/thisType/thisTypeErrors.ts(35,19): error TS2526: 'this' type is available only in a non-static member of a class or interface. +tests/cases/conformance/types/thisType/thisTypeErrors.ts(36,20): error TS2331: 'this' cannot be referenced in a module or namespace body. +tests/cases/conformance/types/thisType/thisTypeErrors.ts(41,14): error TS2526: 'this' type is available only in a non-static member of a class or interface. +tests/cases/conformance/types/thisType/thisTypeErrors.ts(41,21): error TS2526: 'this' type is available only in a non-static member of a class or interface. +tests/cases/conformance/types/thisType/thisTypeErrors.ts(46,23): error TS2526: 'this' type is available only in a non-static member of a class or interface. +tests/cases/conformance/types/thisType/thisTypeErrors.ts(46,30): error TS2526: 'this' type is available only in a non-static member of a class or interface. +tests/cases/conformance/types/thisType/thisTypeErrors.ts(50,18): error TS2526: 'this' type is available only in a non-static member of a class or interface. +tests/cases/conformance/types/thisType/thisTypeErrors.ts(50,25): error TS2526: 'this' type is available only in a non-static member of a class or interface. + + +==== tests/cases/conformance/types/thisType/thisTypeErrors.ts (30 errors) ==== + var x1: this; + ~~~~ +!!! error TS2526: 'this' type is available only in a non-static member of a class or interface. + var x2: { a: this }; + ~~~~ +!!! error TS2526: 'this' type is available only in a non-static member of a class or interface. + var x3: this[]; + ~~~~ +!!! error TS2526: 'this' type is available only in a non-static member of a class or interface. + + function f1(x: this): this { + ~~~~ +!!! error TS2526: 'this' type is available only in a non-static member of a class or interface. + ~~~~ +!!! error TS2526: 'this' type is available only in a non-static member of a class or interface. + var y: this; + ~~~~ +!!! error TS2526: 'this' type is available only in a non-static member of a class or interface. + return this; + } + + interface I1 { + a: { x: this }; + ~~~~ +!!! error TS2526: 'this' type is available only in a non-static member of a class or interface. + b: { (): this }; + ~~~~ +!!! error TS2526: 'this' type is available only in a non-static member of a class or interface. + c: { new (): this }; + ~~~~ +!!! error TS2526: 'this' type is available only in a non-static member of a class or interface. + d: { [x: string]: this }; + ~~~~ +!!! error TS2526: 'this' type is available only in a non-static member of a class or interface. + e: { f(x: this): this }; + ~~~~ +!!! error TS2526: 'this' type is available only in a non-static member of a class or interface. + ~~~~ +!!! error TS2526: 'this' type is available only in a non-static member of a class or interface. + } + + class C1 { + a: { x: this }; + ~~~~ +!!! error TS2526: 'this' type is available only in a non-static member of a class or interface. + b: { (): this }; + ~~~~ +!!! error TS2526: 'this' type is available only in a non-static member of a class or interface. + c: { new (): this }; + ~~~~ +!!! error TS2526: 'this' type is available only in a non-static member of a class or interface. + d: { [x: string]: this }; + ~~~~ +!!! error TS2526: 'this' type is available only in a non-static member of a class or interface. + e: { f(x: this): this }; + ~~~~ +!!! error TS2526: 'this' type is available only in a non-static member of a class or interface. + ~~~~ +!!! error TS2526: 'this' type is available only in a non-static member of a class or interface. + } + + class C2 { + static x: this; + ~~~~ +!!! error TS2526: 'this' type is available only in a non-static member of a class or interface. + static y = undefined; + ~~~~ +!!! error TS2526: 'this' type is available only in a non-static member of a class or interface. + static foo(x: this): this { + ~~~~ +!!! error TS2526: 'this' type is available only in a non-static member of a class or interface. + ~~~~ +!!! error TS2526: 'this' type is available only in a non-static member of a class or interface. + return undefined; + } + } + + namespace N1 { + export var x: this; + ~~~~ +!!! error TS2526: 'this' type is available only in a non-static member of a class or interface. + export var y = this; + ~~~~ +!!! error TS2331: 'this' cannot be referenced in a module or namespace body. + } + + class C3 { + x1 = { + g(x: this): this { + ~~~~ +!!! error TS2526: 'this' type is available only in a non-static member of a class or interface. + ~~~~ +!!! error TS2526: 'this' type is available only in a non-static member of a class or interface. + return undefined; + } + } + f() { + function g(x: this): this { + ~~~~ +!!! error TS2526: 'this' type is available only in a non-static member of a class or interface. + ~~~~ +!!! error TS2526: 'this' type is available only in a non-static member of a class or interface. + return undefined; + } + let x2 = { + h(x: this): this { + ~~~~ +!!! error TS2526: 'this' type is available only in a non-static member of a class or interface. + ~~~~ +!!! error TS2526: 'this' type is available only in a non-static member of a class or interface. + return undefined; + } + } + } + } + \ No newline at end of file diff --git a/tests/baselines/reference/thisTypeErrors.js b/tests/baselines/reference/thisTypeErrors.js new file mode 100644 index 00000000000..677feb374e1 --- /dev/null +++ b/tests/baselines/reference/thisTypeErrors.js @@ -0,0 +1,104 @@ +//// [thisTypeErrors.ts] +var x1: this; +var x2: { a: this }; +var x3: this[]; + +function f1(x: this): this { + var y: this; + return this; +} + +interface I1 { + a: { x: this }; + b: { (): this }; + c: { new (): this }; + d: { [x: string]: this }; + e: { f(x: this): this }; +} + +class C1 { + a: { x: this }; + b: { (): this }; + c: { new (): this }; + d: { [x: string]: this }; + e: { f(x: this): this }; +} + +class C2 { + static x: this; + static y = undefined; + static foo(x: this): this { + return undefined; + } +} + +namespace N1 { + export var x: this; + export var y = this; +} + +class C3 { + x1 = { + g(x: this): this { + return undefined; + } + } + f() { + function g(x: this): this { + return undefined; + } + let x2 = { + h(x: this): this { + return undefined; + } + } + } +} + + +//// [thisTypeErrors.js] +var x1; +var x2; +var x3; +function f1(x) { + var y; + return this; +} +var C1 = (function () { + function C1() { + } + return C1; +})(); +var C2 = (function () { + function C2() { + } + C2.foo = function (x) { + return undefined; + }; + C2.y = undefined; + return C2; +})(); +var N1; +(function (N1) { + N1.y = this; +})(N1 || (N1 = {})); +var C3 = (function () { + function C3() { + this.x1 = { + g: function (x) { + return undefined; + } + }; + } + C3.prototype.f = function () { + function g(x) { + return undefined; + } + var x2 = { + h: function (x) { + return undefined; + } + }; + }; + return C3; +})(); diff --git a/tests/baselines/reference/thisTypeInClasses.js b/tests/baselines/reference/thisTypeInClasses.js new file mode 100644 index 00000000000..93344fa62cb --- /dev/null +++ b/tests/baselines/reference/thisTypeInClasses.js @@ -0,0 +1,91 @@ +//// [thisTypeInClasses.ts] +class C1 { + x: this; + f(x: this): this { return undefined; } + constructor(x: this) { } +} + +class C2 { + [x: string]: this; +} + +interface Foo { + x: T; + y: this; +} + +class C3 { + a: this[]; + b: [this, this]; + c: this | Date; + d: this & Date; + e: (((this))); + f: (x: this) => this; + g: new (x: this) => this; + h: Foo; + i: Foo this)>; + j: (x: any) => x is this; +} + +declare class C4 { + x: this; + f(x: this): this; +} + +class C5 { + foo() { + let f1 = (x: this): this => this; + let f2 = (x: this) => this; + let f3 = (x: this) => (y: this) => this; + let f4 = (x: this) => { + let g = (y: this) => { + return () => this; + } + return g(this); + } + } + bar() { + let x1 = undefined; + let x2 = undefined as this; + } +} + + +//// [thisTypeInClasses.js] +var C1 = (function () { + function C1(x) { + } + C1.prototype.f = function (x) { return undefined; }; + return C1; +})(); +var C2 = (function () { + function C2() { + } + return C2; +})(); +var C3 = (function () { + function C3() { + } + return C3; +})(); +var C5 = (function () { + function C5() { + } + C5.prototype.foo = function () { + var _this = this; + var f1 = function (x) { return _this; }; + var f2 = function (x) { return _this; }; + var f3 = function (x) { return function (y) { return _this; }; }; + var f4 = function (x) { + var g = function (y) { + return function () { return _this; }; + }; + return g(_this); + }; + }; + C5.prototype.bar = function () { + var x1 = undefined; + var x2 = undefined; + }; + return C5; +})(); diff --git a/tests/baselines/reference/thisTypeInClasses.symbols b/tests/baselines/reference/thisTypeInClasses.symbols new file mode 100644 index 00000000000..6383bb01b8d --- /dev/null +++ b/tests/baselines/reference/thisTypeInClasses.symbols @@ -0,0 +1,140 @@ +=== tests/cases/conformance/types/thisType/thisTypeInClasses.ts === +class C1 { +>C1 : Symbol(C1, Decl(thisTypeInClasses.ts, 0, 0)) + + x: this; +>x : Symbol(x, Decl(thisTypeInClasses.ts, 0, 10)) + + f(x: this): this { return undefined; } +>f : Symbol(f, Decl(thisTypeInClasses.ts, 1, 12)) +>x : Symbol(x, Decl(thisTypeInClasses.ts, 2, 6)) +>undefined : Symbol(undefined) + + constructor(x: this) { } +>x : Symbol(x, Decl(thisTypeInClasses.ts, 3, 16)) +} + +class C2 { +>C2 : Symbol(C2, Decl(thisTypeInClasses.ts, 4, 1)) + + [x: string]: this; +>x : Symbol(x, Decl(thisTypeInClasses.ts, 7, 5)) +} + +interface Foo { +>Foo : Symbol(Foo, Decl(thisTypeInClasses.ts, 8, 1)) +>T : Symbol(T, Decl(thisTypeInClasses.ts, 10, 14)) + + x: T; +>x : Symbol(x, Decl(thisTypeInClasses.ts, 10, 18)) +>T : Symbol(T, Decl(thisTypeInClasses.ts, 10, 14)) + + y: this; +>y : Symbol(y, Decl(thisTypeInClasses.ts, 11, 9)) +} + +class C3 { +>C3 : Symbol(C3, Decl(thisTypeInClasses.ts, 13, 1)) + + a: this[]; +>a : Symbol(a, Decl(thisTypeInClasses.ts, 15, 10)) + + b: [this, this]; +>b : Symbol(b, Decl(thisTypeInClasses.ts, 16, 14)) + + c: this | Date; +>c : Symbol(c, Decl(thisTypeInClasses.ts, 17, 20)) +>Date : Symbol(Date, Decl(lib.d.ts, 633, 23), Decl(lib.d.ts, 815, 11)) + + d: this & Date; +>d : Symbol(d, Decl(thisTypeInClasses.ts, 18, 19)) +>Date : Symbol(Date, Decl(lib.d.ts, 633, 23), Decl(lib.d.ts, 815, 11)) + + e: (((this))); +>e : Symbol(e, Decl(thisTypeInClasses.ts, 19, 19)) + + f: (x: this) => this; +>f : Symbol(f, Decl(thisTypeInClasses.ts, 20, 18)) +>x : Symbol(x, Decl(thisTypeInClasses.ts, 21, 8)) + + g: new (x: this) => this; +>g : Symbol(g, Decl(thisTypeInClasses.ts, 21, 25)) +>x : Symbol(x, Decl(thisTypeInClasses.ts, 22, 12)) + + h: Foo; +>h : Symbol(h, Decl(thisTypeInClasses.ts, 22, 29)) +>Foo : Symbol(Foo, Decl(thisTypeInClasses.ts, 8, 1)) + + i: Foo this)>; +>i : Symbol(i, Decl(thisTypeInClasses.ts, 23, 17)) +>Foo : Symbol(Foo, Decl(thisTypeInClasses.ts, 8, 1)) + + j: (x: any) => x is this; +>j : Symbol(j, Decl(thisTypeInClasses.ts, 24, 32)) +>x : Symbol(x, Decl(thisTypeInClasses.ts, 25, 8)) +>x : Symbol(x, Decl(thisTypeInClasses.ts, 25, 8)) +} + +declare class C4 { +>C4 : Symbol(C4, Decl(thisTypeInClasses.ts, 26, 1)) + + x: this; +>x : Symbol(x, Decl(thisTypeInClasses.ts, 28, 18)) + + f(x: this): this; +>f : Symbol(f, Decl(thisTypeInClasses.ts, 29, 12)) +>x : Symbol(x, Decl(thisTypeInClasses.ts, 30, 6)) +} + +class C5 { +>C5 : Symbol(C5, Decl(thisTypeInClasses.ts, 31, 1)) + + foo() { +>foo : Symbol(foo, Decl(thisTypeInClasses.ts, 33, 10)) + + let f1 = (x: this): this => this; +>f1 : Symbol(f1, Decl(thisTypeInClasses.ts, 35, 11)) +>x : Symbol(x, Decl(thisTypeInClasses.ts, 35, 18)) +>this : Symbol(C5, Decl(thisTypeInClasses.ts, 31, 1)) +>this : Symbol(C5, Decl(thisTypeInClasses.ts, 31, 1)) + + let f2 = (x: this) => this; +>f2 : Symbol(f2, Decl(thisTypeInClasses.ts, 36, 11)) +>x : Symbol(x, Decl(thisTypeInClasses.ts, 36, 18)) +>this : Symbol(C5, Decl(thisTypeInClasses.ts, 31, 1)) + + let f3 = (x: this) => (y: this) => this; +>f3 : Symbol(f3, Decl(thisTypeInClasses.ts, 37, 11)) +>x : Symbol(x, Decl(thisTypeInClasses.ts, 37, 18)) +>y : Symbol(y, Decl(thisTypeInClasses.ts, 37, 31)) +>this : Symbol(C5, Decl(thisTypeInClasses.ts, 31, 1)) + + let f4 = (x: this) => { +>f4 : Symbol(f4, Decl(thisTypeInClasses.ts, 38, 11)) +>x : Symbol(x, Decl(thisTypeInClasses.ts, 38, 18)) + + let g = (y: this) => { +>g : Symbol(g, Decl(thisTypeInClasses.ts, 39, 15)) +>y : Symbol(y, Decl(thisTypeInClasses.ts, 39, 21)) + + return () => this; +>this : Symbol(C5, Decl(thisTypeInClasses.ts, 31, 1)) + } + return g(this); +>g : Symbol(g, Decl(thisTypeInClasses.ts, 39, 15)) +>this : Symbol(C5, Decl(thisTypeInClasses.ts, 31, 1)) + } + } + bar() { +>bar : Symbol(bar, Decl(thisTypeInClasses.ts, 44, 5)) + + let x1 = undefined; +>x1 : Symbol(x1, Decl(thisTypeInClasses.ts, 46, 11)) +>undefined : Symbol(undefined) + + let x2 = undefined as this; +>x2 : Symbol(x2, Decl(thisTypeInClasses.ts, 47, 11)) +>undefined : Symbol(undefined) + } +} + diff --git a/tests/baselines/reference/thisTypeInClasses.types b/tests/baselines/reference/thisTypeInClasses.types new file mode 100644 index 00000000000..ddfdfb277a9 --- /dev/null +++ b/tests/baselines/reference/thisTypeInClasses.types @@ -0,0 +1,150 @@ +=== tests/cases/conformance/types/thisType/thisTypeInClasses.ts === +class C1 { +>C1 : C1 + + x: this; +>x : this + + f(x: this): this { return undefined; } +>f : (x: this) => this +>x : this +>undefined : undefined + + constructor(x: this) { } +>x : this +} + +class C2 { +>C2 : C2 + + [x: string]: this; +>x : string +} + +interface Foo { +>Foo : Foo +>T : T + + x: T; +>x : T +>T : T + + y: this; +>y : this +} + +class C3 { +>C3 : C3 + + a: this[]; +>a : this[] + + b: [this, this]; +>b : [this, this] + + c: this | Date; +>c : this | Date +>Date : Date + + d: this & Date; +>d : this & Date +>Date : Date + + e: (((this))); +>e : this + + f: (x: this) => this; +>f : (x: this) => this +>x : this + + g: new (x: this) => this; +>g : new (x: this) => this +>x : this + + h: Foo; +>h : Foo +>Foo : Foo + + i: Foo this)>; +>i : Foo this)> +>Foo : Foo + + j: (x: any) => x is this; +>j : (x: any) => x is this +>x : any +>x : any +} + +declare class C4 { +>C4 : C4 + + x: this; +>x : this + + f(x: this): this; +>f : (x: this) => this +>x : this +} + +class C5 { +>C5 : C5 + + foo() { +>foo : () => void + + let f1 = (x: this): this => this; +>f1 : (x: this) => this +>(x: this): this => this : (x: this) => this +>x : this +>this : this +>this : this + + let f2 = (x: this) => this; +>f2 : (x: this) => this +>(x: this) => this : (x: this) => this +>x : this +>this : this + + let f3 = (x: this) => (y: this) => this; +>f3 : (x: this) => (y: this) => this +>(x: this) => (y: this) => this : (x: this) => (y: this) => this +>x : this +>(y: this) => this : (y: this) => this +>y : this +>this : this + + let f4 = (x: this) => { +>f4 : (x: this) => () => this +>(x: this) => { let g = (y: this) => { return () => this; } return g(this); } : (x: this) => () => this +>x : this + + let g = (y: this) => { +>g : (y: this) => () => this +>(y: this) => { return () => this; } : (y: this) => () => this +>y : this + + return () => this; +>() => this : () => this +>this : this + } + return g(this); +>g(this) : () => this +>g : (y: this) => () => this +>this : this + } + } + bar() { +>bar : () => void + + let x1 = undefined; +>x1 : this +>undefined : this +>undefined : undefined + + let x2 = undefined as this; +>x2 : this +>undefined as this : this +>undefined : undefined + } +} + diff --git a/tests/baselines/reference/thisTypeInInterfaces.js b/tests/baselines/reference/thisTypeInInterfaces.js new file mode 100644 index 00000000000..eb521918d33 --- /dev/null +++ b/tests/baselines/reference/thisTypeInInterfaces.js @@ -0,0 +1,32 @@ +//// [thisTypeInInterfaces.ts] +interface I1 { + x: this; + f(x: this): this; +} + +interface I2 { + (x: this): this; + new (x: this): this; + [x: string]: this; +} + +interface Foo { + x: T; + y: this; +} + +interface I3 { + a: this[]; + b: [this, this]; + c: this | Date; + d: this & Date; + e: (((this))); + f: (x: this) => this; + g: new (x: this) => this; + h: Foo; + i: Foo this)>; + j: (x: any) => x is this; +} + + +//// [thisTypeInInterfaces.js] diff --git a/tests/baselines/reference/thisTypeInInterfaces.symbols b/tests/baselines/reference/thisTypeInInterfaces.symbols new file mode 100644 index 00000000000..498fbcc8796 --- /dev/null +++ b/tests/baselines/reference/thisTypeInInterfaces.symbols @@ -0,0 +1,79 @@ +=== tests/cases/conformance/types/thisType/thisTypeInInterfaces.ts === +interface I1 { +>I1 : Symbol(I1, Decl(thisTypeInInterfaces.ts, 0, 0)) + + x: this; +>x : Symbol(x, Decl(thisTypeInInterfaces.ts, 0, 14)) + + f(x: this): this; +>f : Symbol(f, Decl(thisTypeInInterfaces.ts, 1, 12)) +>x : Symbol(x, Decl(thisTypeInInterfaces.ts, 2, 6)) +} + +interface I2 { +>I2 : Symbol(I2, Decl(thisTypeInInterfaces.ts, 3, 1)) + + (x: this): this; +>x : Symbol(x, Decl(thisTypeInInterfaces.ts, 6, 5)) + + new (x: this): this; +>x : Symbol(x, Decl(thisTypeInInterfaces.ts, 7, 9)) + + [x: string]: this; +>x : Symbol(x, Decl(thisTypeInInterfaces.ts, 8, 5)) +} + +interface Foo { +>Foo : Symbol(Foo, Decl(thisTypeInInterfaces.ts, 9, 1)) +>T : Symbol(T, Decl(thisTypeInInterfaces.ts, 11, 14)) + + x: T; +>x : Symbol(x, Decl(thisTypeInInterfaces.ts, 11, 18)) +>T : Symbol(T, Decl(thisTypeInInterfaces.ts, 11, 14)) + + y: this; +>y : Symbol(y, Decl(thisTypeInInterfaces.ts, 12, 9)) +} + +interface I3 { +>I3 : Symbol(I3, Decl(thisTypeInInterfaces.ts, 14, 1)) + + a: this[]; +>a : Symbol(a, Decl(thisTypeInInterfaces.ts, 16, 14)) + + b: [this, this]; +>b : Symbol(b, Decl(thisTypeInInterfaces.ts, 17, 14)) + + c: this | Date; +>c : Symbol(c, Decl(thisTypeInInterfaces.ts, 18, 20)) +>Date : Symbol(Date, Decl(lib.d.ts, 633, 23), Decl(lib.d.ts, 815, 11)) + + d: this & Date; +>d : Symbol(d, Decl(thisTypeInInterfaces.ts, 19, 19)) +>Date : Symbol(Date, Decl(lib.d.ts, 633, 23), Decl(lib.d.ts, 815, 11)) + + e: (((this))); +>e : Symbol(e, Decl(thisTypeInInterfaces.ts, 20, 19)) + + f: (x: this) => this; +>f : Symbol(f, Decl(thisTypeInInterfaces.ts, 21, 18)) +>x : Symbol(x, Decl(thisTypeInInterfaces.ts, 22, 8)) + + g: new (x: this) => this; +>g : Symbol(g, Decl(thisTypeInInterfaces.ts, 22, 25)) +>x : Symbol(x, Decl(thisTypeInInterfaces.ts, 23, 12)) + + h: Foo; +>h : Symbol(h, Decl(thisTypeInInterfaces.ts, 23, 29)) +>Foo : Symbol(Foo, Decl(thisTypeInInterfaces.ts, 9, 1)) + + i: Foo this)>; +>i : Symbol(i, Decl(thisTypeInInterfaces.ts, 24, 17)) +>Foo : Symbol(Foo, Decl(thisTypeInInterfaces.ts, 9, 1)) + + j: (x: any) => x is this; +>j : Symbol(j, Decl(thisTypeInInterfaces.ts, 25, 32)) +>x : Symbol(x, Decl(thisTypeInInterfaces.ts, 26, 8)) +>x : Symbol(x, Decl(thisTypeInInterfaces.ts, 26, 8)) +} + diff --git a/tests/baselines/reference/thisTypeInInterfaces.types b/tests/baselines/reference/thisTypeInInterfaces.types new file mode 100644 index 00000000000..5eadd7c13be --- /dev/null +++ b/tests/baselines/reference/thisTypeInInterfaces.types @@ -0,0 +1,79 @@ +=== tests/cases/conformance/types/thisType/thisTypeInInterfaces.ts === +interface I1 { +>I1 : I1 + + x: this; +>x : this + + f(x: this): this; +>f : (x: this) => this +>x : this +} + +interface I2 { +>I2 : I2 + + (x: this): this; +>x : this + + new (x: this): this; +>x : this + + [x: string]: this; +>x : string +} + +interface Foo { +>Foo : Foo +>T : T + + x: T; +>x : T +>T : T + + y: this; +>y : this +} + +interface I3 { +>I3 : I3 + + a: this[]; +>a : this[] + + b: [this, this]; +>b : [this, this] + + c: this | Date; +>c : this | Date +>Date : Date + + d: this & Date; +>d : this & Date +>Date : Date + + e: (((this))); +>e : this + + f: (x: this) => this; +>f : (x: this) => this +>x : this + + g: new (x: this) => this; +>g : new (x: this) => this +>x : this + + h: Foo; +>h : Foo +>Foo : Foo + + i: Foo this)>; +>i : Foo this)> +>Foo : Foo + + j: (x: any) => x is this; +>j : (x: any) => x is this +>x : any +>x : any +} + diff --git a/tests/baselines/reference/typeRelationships.errors.txt b/tests/baselines/reference/typeRelationships.errors.txt new file mode 100644 index 00000000000..37954415778 --- /dev/null +++ b/tests/baselines/reference/typeRelationships.errors.txt @@ -0,0 +1,54 @@ +tests/cases/conformance/types/thisType/typeRelationships.ts(9,9): error TS2322: Type 'C' is not assignable to type 'this'. +tests/cases/conformance/types/thisType/typeRelationships.ts(35,9): error TS2322: Type 'C' is not assignable to type 'D'. + Property 'self1' is missing in type 'C'. +tests/cases/conformance/types/thisType/typeRelationships.ts(36,9): error TS2322: Type 'D' is not assignable to type 'this'. + + +==== tests/cases/conformance/types/thisType/typeRelationships.ts (3 errors) ==== + class C { + self = this; + c = new C(); + foo() { + return this; + } + f1() { + this.c = this.self; + this.self = this.c; // Error + ~~~~~~~~~ +!!! error TS2322: Type 'C' is not assignable to type 'this'. + } + f2() { + var a: C[]; + var a = [this, this.c]; // C[] since this is subtype of C + var b: this[]; + var b = [this, this.self, null, undefined]; + } + f3(b: boolean) { + return b ? this.c : this.self; // Should be C + } + } + + class D extends C { + self1 = this; + self2 = this.self; + self3 = this.foo(); + d = new D(); + bar() { + this.self = this.self1; + this.self = this.self2; + this.self = this.self3; + this.self1 = this.self; + this.self2 = this.self; + this.self3 = this.self; + this.d = this.self; + this.d = this.c; // Error + ~~~~~~ +!!! error TS2322: Type 'C' is not assignable to type 'D'. +!!! error TS2322: Property 'self1' is missing in type 'C'. + this.self = this.d; // Error + ~~~~~~~~~ +!!! error TS2322: Type 'D' is not assignable to type 'this'. + this.c = this.d; + } + } + \ No newline at end of file diff --git a/tests/baselines/reference/typeRelationships.js b/tests/baselines/reference/typeRelationships.js new file mode 100644 index 00000000000..1e31009f9b7 --- /dev/null +++ b/tests/baselines/reference/typeRelationships.js @@ -0,0 +1,94 @@ +//// [typeRelationships.ts] +class C { + self = this; + c = new C(); + foo() { + return this; + } + f1() { + this.c = this.self; + this.self = this.c; // Error + } + f2() { + var a: C[]; + var a = [this, this.c]; // C[] since this is subtype of C + var b: this[]; + var b = [this, this.self, null, undefined]; + } + f3(b: boolean) { + return b ? this.c : this.self; // Should be C + } +} + +class D extends C { + self1 = this; + self2 = this.self; + self3 = this.foo(); + d = new D(); + bar() { + this.self = this.self1; + this.self = this.self2; + this.self = this.self3; + this.self1 = this.self; + this.self2 = this.self; + this.self3 = this.self; + this.d = this.self; + this.d = this.c; // Error + this.self = this.d; // Error + this.c = this.d; + } +} + + +//// [typeRelationships.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var C = (function () { + function C() { + this.self = this; + this.c = new C(); + } + C.prototype.foo = function () { + return this; + }; + C.prototype.f1 = function () { + this.c = this.self; + this.self = this.c; // Error + }; + C.prototype.f2 = function () { + var a; + var a = [this, this.c]; // C[] since this is subtype of C + var b; + var b = [this, this.self, null, undefined]; + }; + C.prototype.f3 = function (b) { + return b ? this.c : this.self; // Should be C + }; + return C; +})(); +var D = (function (_super) { + __extends(D, _super); + function D() { + _super.apply(this, arguments); + this.self1 = this; + this.self2 = this.self; + this.self3 = this.foo(); + this.d = new D(); + } + D.prototype.bar = function () { + this.self = this.self1; + this.self = this.self2; + this.self = this.self3; + this.self1 = this.self; + this.self2 = this.self; + this.self3 = this.self; + this.d = this.self; + this.d = this.c; // Error + this.self = this.d; // Error + this.c = this.d; + }; + return D; +})(C); diff --git a/tests/cases/conformance/types/thisType/fluentClasses.ts b/tests/cases/conformance/types/thisType/fluentClasses.ts new file mode 100644 index 00000000000..c9e791cb6f1 --- /dev/null +++ b/tests/cases/conformance/types/thisType/fluentClasses.ts @@ -0,0 +1,17 @@ +class A { + foo() { + return this; + } +} +class B extends A { + bar() { + return this; + } +} +class C extends B { + baz() { + return this; + } +} +var c: C; +var z = c.foo().bar().baz(); // Fluent pattern diff --git a/tests/cases/conformance/types/thisType/fluentInterfaces.ts b/tests/cases/conformance/types/thisType/fluentInterfaces.ts new file mode 100644 index 00000000000..d5fabd56bf9 --- /dev/null +++ b/tests/cases/conformance/types/thisType/fluentInterfaces.ts @@ -0,0 +1,11 @@ +interface A { + foo(): this; +} +interface B extends A { + bar(): this; +} +interface C extends B { + baz(): this; +} +var c: C; +var z = c.foo().bar().baz(); // Fluent pattern diff --git a/tests/cases/conformance/types/thisType/thisTypeErrors.ts b/tests/cases/conformance/types/thisType/thisTypeErrors.ts new file mode 100644 index 00000000000..a7e4a46493d --- /dev/null +++ b/tests/cases/conformance/types/thisType/thisTypeErrors.ts @@ -0,0 +1,55 @@ +var x1: this; +var x2: { a: this }; +var x3: this[]; + +function f1(x: this): this { + var y: this; + return this; +} + +interface I1 { + a: { x: this }; + b: { (): this }; + c: { new (): this }; + d: { [x: string]: this }; + e: { f(x: this): this }; +} + +class C1 { + a: { x: this }; + b: { (): this }; + c: { new (): this }; + d: { [x: string]: this }; + e: { f(x: this): this }; +} + +class C2 { + static x: this; + static y = undefined; + static foo(x: this): this { + return undefined; + } +} + +namespace N1 { + export var x: this; + export var y = this; +} + +class C3 { + x1 = { + g(x: this): this { + return undefined; + } + } + f() { + function g(x: this): this { + return undefined; + } + let x2 = { + h(x: this): this { + return undefined; + } + } + } +} diff --git a/tests/cases/conformance/types/thisType/thisTypeInClasses.ts b/tests/cases/conformance/types/thisType/thisTypeInClasses.ts new file mode 100644 index 00000000000..17dc13cf558 --- /dev/null +++ b/tests/cases/conformance/types/thisType/thisTypeInClasses.ts @@ -0,0 +1,50 @@ +class C1 { + x: this; + f(x: this): this { return undefined; } + constructor(x: this) { } +} + +class C2 { + [x: string]: this; +} + +interface Foo { + x: T; + y: this; +} + +class C3 { + a: this[]; + b: [this, this]; + c: this | Date; + d: this & Date; + e: (((this))); + f: (x: this) => this; + g: new (x: this) => this; + h: Foo; + i: Foo this)>; + j: (x: any) => x is this; +} + +declare class C4 { + x: this; + f(x: this): this; +} + +class C5 { + foo() { + let f1 = (x: this): this => this; + let f2 = (x: this) => this; + let f3 = (x: this) => (y: this) => this; + let f4 = (x: this) => { + let g = (y: this) => { + return () => this; + } + return g(this); + } + } + bar() { + let x1 = undefined; + let x2 = undefined as this; + } +} diff --git a/tests/cases/conformance/types/thisType/thisTypeInInterfaces.ts b/tests/cases/conformance/types/thisType/thisTypeInInterfaces.ts new file mode 100644 index 00000000000..c1f48ad8f15 --- /dev/null +++ b/tests/cases/conformance/types/thisType/thisTypeInInterfaces.ts @@ -0,0 +1,28 @@ +interface I1 { + x: this; + f(x: this): this; +} + +interface I2 { + (x: this): this; + new (x: this): this; + [x: string]: this; +} + +interface Foo { + x: T; + y: this; +} + +interface I3 { + a: this[]; + b: [this, this]; + c: this | Date; + d: this & Date; + e: (((this))); + f: (x: this) => this; + g: new (x: this) => this; + h: Foo; + i: Foo this)>; + j: (x: any) => x is this; +} diff --git a/tests/cases/conformance/types/thisType/typeRelationships.ts b/tests/cases/conformance/types/thisType/typeRelationships.ts new file mode 100644 index 00000000000..c3ca6f3ef70 --- /dev/null +++ b/tests/cases/conformance/types/thisType/typeRelationships.ts @@ -0,0 +1,39 @@ +class C { + self = this; + c = new C(); + foo() { + return this; + } + f1() { + this.c = this.self; + this.self = this.c; // Error + } + f2() { + var a: C[]; + var a = [this, this.c]; // C[] since this is subtype of C + var b: this[]; + var b = [this, this.self, null, undefined]; + } + f3(b: boolean) { + return b ? this.c : this.self; // Should be C + } +} + +class D extends C { + self1 = this; + self2 = this.self; + self3 = this.foo(); + d = new D(); + bar() { + this.self = this.self1; + this.self = this.self2; + this.self = this.self3; + this.self1 = this.self; + this.self2 = this.self; + this.self3 = this.self; + this.d = this.self; + this.d = this.c; // Error + this.self = this.d; // Error + this.c = this.d; + } +}