Add --noImplicitThis flag

It's basically another --noImplicitAny error, but one that would break
large amount of JavaScript-style code.
This commit is contained in:
Nathan Shively-Sanders
2016-03-25 16:37:28 -07:00
parent c9f5f3d67e
commit a91cdccfc5
7 changed files with 95 additions and 1 deletions

View File

@@ -0,0 +1,25 @@
tests/cases/compiler/noImplicitThisFunctions.ts(14,12): error TS2681: 'this' implicitly has type 'any' because it does not have a type annotation.
==== tests/cases/compiler/noImplicitThisFunctions.ts (1 errors) ====
function f1(x) {
// implicit any is still allowed
return x + 1;
}
function f2(y: number) {
// ok: no reference to this
return y + 1;
}
function f3(z: number): number {
// error: this is implicitly any
return this.a + z;
~~~~
!!! error TS2681: 'this' implicitly has type 'any' because it does not have a type annotation.
}
// ok, arrow functions don't even bind `this`, so `this` is just `window`
let f4: (b: number) => number = b => this.c + b;

View File

@@ -0,0 +1,37 @@
//// [noImplicitThisFunctions.ts]
function f1(x) {
// implicit any is still allowed
return x + 1;
}
function f2(y: number) {
// ok: no reference to this
return y + 1;
}
function f3(z: number): number {
// error: this is implicitly any
return this.a + z;
}
// ok, arrow functions don't even bind `this`, so `this` is just `window`
let f4: (b: number) => number = b => this.c + b;
//// [noImplicitThisFunctions.js]
var _this = this;
function f1(x) {
// implicit any is still allowed
return x + 1;
}
function f2(y) {
// ok: no reference to this
return y + 1;
}
function f3(z) {
// error: this is implicitly any
return this.a + z;
}
// ok, arrow functions don't even bind `this`, so `this` is just `window`
var f4 = function (b) { return _this.c + b; };

View File

@@ -0,0 +1,19 @@
// @noImplicitThis: true
function f1(x) {
// implicit any is still allowed
return x + 1;
}
function f2(y: number) {
// ok: no reference to this
return y + 1;
}
function f3(z: number): number {
// error: this is implicitly any
return this.a + z;
}
// ok, arrow functions don't even bind `this`, so `this` is just `window`
let f4: (b: number) => number = b => this.c + b;