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
|
//// [looseThisTypeInFunctions.ts]
interface I {
n: number;
explicitThis(this: this, m: number): number;
}
interface Unused {
implicitNoThis(m: number): number;
}
class C implements I {
n: number;
explicitThis(this: this, m: number): number {
return this.n + m;
}
implicitThis(m: number): number {
return this.n + m;
}
explicitVoid(this: void, m: number): number {
return m + 1;
}
}
let c = new C();
c.explicitVoid = c.explicitThis; // error, 'void' is missing everything
let o = {
n: 101,
explicitThis: function (m: number) {
return m + this.n.length; // error, 'length' does not exist on 'number'
},
implicitThis(m: number): number { return m; }
};
let i: I = o;
let o2: I = {
n: 1001,
explicitThis: function (m) {
return m + this.n.length; // error, this.n: number, no member 'length'
},
}
let x = i.explicitThis;
let n = x(12); // callee:void doesn't match this:I
let u: Unused;
let y = u.implicitNoThis;
n = y(12); // ok, callee:void matches this:any
c.explicitVoid = c.implicitThis // ok, implicitThis(this:any)
o.implicitThis = c.implicitThis; // ok, implicitThis(this:any)
o.implicitThis = c.explicitThis; // ok, implicitThis(this:any) is assignable to explicitThis(this: this)
o.implicitThis = i.explicitThis;
i.explicitThis = function(m) {
return this.n.length; // error, this.n: number
}
//// [looseThisTypeInFunctions.js]
var C = /** @class */ (function () {
function C() {
}
C.prototype.explicitThis = function (m) {
return this.n + m;
};
C.prototype.implicitThis = function (m) {
return this.n + m;
};
C.prototype.explicitVoid = function (m) {
return m + 1;
};
return C;
}());
var c = new C();
c.explicitVoid = c.explicitThis; // error, 'void' is missing everything
var o = {
n: 101,
explicitThis: function (m) {
return m + this.n.length; // error, 'length' does not exist on 'number'
},
implicitThis: function (m) { return m; }
};
var i = o;
var o2 = {
n: 1001,
explicitThis: function (m) {
return m + this.n.length; // error, this.n: number, no member 'length'
}
};
var x = i.explicitThis;
var n = x(12); // callee:void doesn't match this:I
var u;
var y = u.implicitNoThis;
n = y(12); // ok, callee:void matches this:any
c.explicitVoid = c.implicitThis; // ok, implicitThis(this:any)
o.implicitThis = c.implicitThis; // ok, implicitThis(this:any)
o.implicitThis = c.explicitThis; // ok, implicitThis(this:any) is assignable to explicitThis(this: this)
o.implicitThis = i.explicitThis;
i.explicitThis = function (m) {
return this.n.length; // error, this.n: number
};
|