mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-15 03:23:08 -06: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:
parent
c9f5f3d67e
commit
a91cdccfc5
@ -7707,6 +7707,10 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
|
||||
if (compilerOptions.noImplicitThis && isFunctionLike(container)) {
|
||||
// With noImplicitThis, functions may not reference 'this' if it has type 'any'
|
||||
error(node, Diagnostics.this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation);
|
||||
}
|
||||
return anyType;
|
||||
}
|
||||
|
||||
|
||||
@ -125,6 +125,10 @@ namespace ts {
|
||||
type: "boolean",
|
||||
description: Diagnostics.Raise_error_on_expressions_and_declarations_with_an_implied_any_type,
|
||||
},
|
||||
{
|
||||
name: "noImplicitThis",
|
||||
type: "boolean",
|
||||
},
|
||||
{
|
||||
name: "noLib",
|
||||
type: "boolean",
|
||||
|
||||
@ -1863,7 +1863,11 @@
|
||||
"category": "Error",
|
||||
"code": 2680
|
||||
},
|
||||
"Import declaration '{0}' is using private name '{1}'.": {
|
||||
"'this' implicitly has type 'any' because it does not have a type annotation.": {
|
||||
"category": "Error",
|
||||
"code": 2681
|
||||
},
|
||||
"Import declaration '{0}' is using private name '{1}'.": {
|
||||
"category": "Error",
|
||||
"code": 4000
|
||||
},
|
||||
|
||||
@ -2395,6 +2395,7 @@ namespace ts {
|
||||
noEmitOnError?: boolean;
|
||||
noErrorTruncation?: boolean;
|
||||
noImplicitAny?: boolean;
|
||||
noImplicitThis?: boolean;
|
||||
noLib?: boolean;
|
||||
noResolve?: boolean;
|
||||
out?: string;
|
||||
|
||||
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;
|
||||
Loading…
x
Reference in New Issue
Block a user