Skip to content

Commit 0b95731

Browse files
authored
Fix 10408 : Better error message for set/get with noImplicitAny error (microsoft#10597)
* Giving more explicit error message when there is no-implicit-any on get/set accessor * Update error message number * Add new test and baselines * Address PR: assert that getter must existed * Address PR: undo renumbering of error messages
1 parent 0485bb6 commit 0b95731

9 files changed

+176
-8
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3346,7 +3346,13 @@ namespace ts {
33463346
// Otherwise, fall back to 'any'.
33473347
else {
33483348
if (compilerOptions.noImplicitAny) {
3349-
error(setter, Diagnostics.Property_0_implicitly_has_type_any_because_its_set_accessor_lacks_a_type_annotation, symbolToString(symbol));
3349+
if (setter) {
3350+
error(setter, Diagnostics.Property_0_implicitly_has_type_any_because_its_set_accessor_lacks_a_parameter_type_annotation, symbolToString(symbol));
3351+
}
3352+
else {
3353+
Debug.assert(!!getter, "there must existed getter as we are current checking either setter or getter in this function");
3354+
error(getter, Diagnostics.Property_0_implicitly_has_type_any_because_its_get_accessor_lacks_a_return_type_annotation, symbolToString(symbol));
3355+
}
33503356
}
33513357
type = anyType;
33523358
}

src/compiler/diagnosticMessages.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2871,11 +2871,7 @@
28712871
"Element implicitly has an 'any' type because index expression is not of type 'number'.": {
28722872
"category": "Error",
28732873
"code": 7015
2874-
},
2875-
"Property '{0}' implicitly has type 'any', because its 'set' accessor lacks a type annotation.": {
2876-
"category": "Error",
2877-
"code": 7016
2878-
},
2874+
},
28792875
"Index signature of object type implicitly has an 'any' type.": {
28802876
"category": "Error",
28812877
"code": 7017
@@ -2932,6 +2928,14 @@
29322928
"category": "Error",
29332929
"code": 7031
29342930
},
2931+
"Property '{0}' implicitly has type 'any', because its set accessor lacks a parameter type annotation.": {
2932+
"category": "Error",
2933+
"code": 7032
2934+
},
2935+
"Property '{0}' implicitly has type 'any', because its get accessor lacks a return type annotation.": {
2936+
"category": "Error",
2937+
"code": 7033
2938+
},
29352939
"You cannot rename this element.": {
29362940
"category": "Error",
29372941
"code": 8000

tests/baselines/reference/implicitAnyGetAndSetAccessorWithAnyReturnType.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ tests/cases/compiler/implicitAnyGetAndSetAccessorWithAnyReturnType.ts(3,5): erro
22
tests/cases/compiler/implicitAnyGetAndSetAccessorWithAnyReturnType.ts(4,16): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
33
tests/cases/compiler/implicitAnyGetAndSetAccessorWithAnyReturnType.ts(9,16): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
44
tests/cases/compiler/implicitAnyGetAndSetAccessorWithAnyReturnType.ts(15,16): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
5-
tests/cases/compiler/implicitAnyGetAndSetAccessorWithAnyReturnType.ts(15,16): error TS7016: Property 'haveOnlySet' implicitly has type 'any', because its 'set' accessor lacks a type annotation.
5+
tests/cases/compiler/implicitAnyGetAndSetAccessorWithAnyReturnType.ts(15,16): error TS7032: Property 'haveOnlySet' implicitly has type 'any', because its set accessor lacks a parameter type annotation.
66
tests/cases/compiler/implicitAnyGetAndSetAccessorWithAnyReturnType.ts(15,28): error TS7006: Parameter 'newXValue' implicitly has an 'any' type.
77
tests/cases/compiler/implicitAnyGetAndSetAccessorWithAnyReturnType.ts(20,16): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
88
tests/cases/compiler/implicitAnyGetAndSetAccessorWithAnyReturnType.ts(20,16): error TS7010: 'haveOnlyGet', which lacks return-type annotation, implicitly has an 'any' return type.
@@ -33,7 +33,7 @@ tests/cases/compiler/implicitAnyGetAndSetAccessorWithAnyReturnType.ts(20,16): er
3333
~~~~~~~~~~~
3434
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
3535
~~~~~~~~~~~
36-
!!! error TS7016: Property 'haveOnlySet' implicitly has type 'any', because its 'set' accessor lacks a type annotation.
36+
!!! error TS7032: Property 'haveOnlySet' implicitly has type 'any', because its set accessor lacks a parameter type annotation.
3737
~~~~~~~~~
3838
!!! error TS7006: Parameter 'newXValue' implicitly has an 'any' type.
3939
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
tests/cases/compiler/noImplicitAnyMissingGetAccessor.ts(4,25): error TS7032: Property 'message' implicitly has type 'any', because its set accessor lacks a parameter type annotation.
2+
tests/cases/compiler/noImplicitAnyMissingGetAccessor.ts(4,33): error TS7006: Parameter 'str' implicitly has an 'any' type.
3+
tests/cases/compiler/noImplicitAnyMissingGetAccessor.ts(9,16): error TS7032: Property 'message' implicitly has type 'any', because its set accessor lacks a parameter type annotation.
4+
tests/cases/compiler/noImplicitAnyMissingGetAccessor.ts(9,24): error TS7006: Parameter 'str' implicitly has an 'any' type.
5+
6+
7+
==== tests/cases/compiler/noImplicitAnyMissingGetAccessor.ts (4 errors) ====
8+
9+
abstract class Parent
10+
{
11+
public abstract set message(str);
12+
~~~~~~~
13+
!!! error TS7032: Property 'message' implicitly has type 'any', because its set accessor lacks a parameter type annotation.
14+
~~~
15+
!!! error TS7006: Parameter 'str' implicitly has an 'any' type.
16+
}
17+
18+
class Child extends Parent {
19+
_x: any;
20+
public set message(str) {
21+
~~~~~~~
22+
!!! error TS7032: Property 'message' implicitly has type 'any', because its set accessor lacks a parameter type annotation.
23+
~~~
24+
!!! error TS7006: Parameter 'str' implicitly has an 'any' type.
25+
this._x = str;
26+
}
27+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//// [noImplicitAnyMissingGetAccessor.ts]
2+
3+
abstract class Parent
4+
{
5+
public abstract set message(str);
6+
}
7+
8+
class Child extends Parent {
9+
_x: any;
10+
public set message(str) {
11+
this._x = str;
12+
}
13+
}
14+
15+
//// [noImplicitAnyMissingGetAccessor.js]
16+
var __extends = (this && this.__extends) || function (d, b) {
17+
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
18+
function __() { this.constructor = d; }
19+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
20+
};
21+
var Parent = (function () {
22+
function Parent() {
23+
}
24+
Object.defineProperty(Parent.prototype, "message", {
25+
set: function (str) { },
26+
enumerable: true,
27+
configurable: true
28+
});
29+
return Parent;
30+
}());
31+
var Child = (function (_super) {
32+
__extends(Child, _super);
33+
function Child() {
34+
_super.apply(this, arguments);
35+
}
36+
Object.defineProperty(Child.prototype, "message", {
37+
set: function (str) {
38+
this._x = str;
39+
},
40+
enumerable: true,
41+
configurable: true
42+
});
43+
return Child;
44+
}(Parent));
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
tests/cases/compiler/noImplicitAnyMissingSetAccessor.ts(4,25): error TS7033: Property 'message' implicitly has type 'any', because its get accessor lacks a return type annotation.
2+
3+
4+
==== tests/cases/compiler/noImplicitAnyMissingSetAccessor.ts (1 errors) ====
5+
6+
abstract class Parent
7+
{
8+
public abstract get message();
9+
~~~~~~~
10+
!!! error TS7033: Property 'message' implicitly has type 'any', because its get accessor lacks a return type annotation.
11+
}
12+
13+
class Child extends Parent {
14+
public get message() {
15+
return "";
16+
}
17+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//// [noImplicitAnyMissingSetAccessor.ts]
2+
3+
abstract class Parent
4+
{
5+
public abstract get message();
6+
}
7+
8+
class Child extends Parent {
9+
public get message() {
10+
return "";
11+
}
12+
}
13+
14+
//// [noImplicitAnyMissingSetAccessor.js]
15+
var __extends = (this && this.__extends) || function (d, b) {
16+
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
17+
function __() { this.constructor = d; }
18+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
19+
};
20+
var Parent = (function () {
21+
function Parent() {
22+
}
23+
Object.defineProperty(Parent.prototype, "message", {
24+
get: function () { },
25+
enumerable: true,
26+
configurable: true
27+
});
28+
return Parent;
29+
}());
30+
var Child = (function (_super) {
31+
__extends(Child, _super);
32+
function Child() {
33+
_super.apply(this, arguments);
34+
}
35+
Object.defineProperty(Child.prototype, "message", {
36+
get: function () {
37+
return "";
38+
},
39+
enumerable: true,
40+
configurable: true
41+
});
42+
return Child;
43+
}(Parent));
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// @noImplicitAny : true
2+
// @target: es5
3+
4+
abstract class Parent
5+
{
6+
public abstract set message(str);
7+
}
8+
9+
class Child extends Parent {
10+
_x: any;
11+
public set message(str) {
12+
this._x = str;
13+
}
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @noImplicitAny: true
2+
// @target: es5
3+
4+
abstract class Parent
5+
{
6+
public abstract get message();
7+
}
8+
9+
class Child extends Parent {
10+
public get message() {
11+
return "";
12+
}
13+
}

0 commit comments

Comments
 (0)