mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-22 12:03:44 -05:00
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:
25
tests/baselines/reference/noImplicitThisFunctions.errors.txt
Normal file
25
tests/baselines/reference/noImplicitThisFunctions.errors.txt
Normal 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;
|
||||
|
||||
37
tests/baselines/reference/noImplicitThisFunctions.js
Normal file
37
tests/baselines/reference/noImplicitThisFunctions.js
Normal 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; };
|
||||
19
tests/cases/compiler/noImplicitThisFunctions.ts
Normal file
19
tests/cases/compiler/noImplicitThisFunctions.ts
Normal 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;
|
||||
Reference in New Issue
Block a user