Skip to content

Commit d39e424

Browse files
committed
microsoft#25002: Implemented changes from feedback
1 parent bf1af7a commit d39e424

9 files changed

+27
-52
lines changed

src/compiler/checker.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -22046,7 +22046,7 @@ namespace ts {
2204622046

2204722047
// TypeScript 1.0 spec (April 2014): 4.5
2204822048
// If both accessors include type annotations, the specified types must be identical.
22049-
checkAccessorDeclarationTypesIdentical(node, otherAccessor, getAnnotatedAccessorType, Diagnostics.get_and_set_accessor_must_have_the_same_type_0_but_this_get_accessor_has_the_type_1);
22049+
checkAccessorDeclarationTypesIdentical(node, otherAccessor, getAnnotatedAccessorType, Diagnostics.get_and_set_accessor_must_have_the_same_type_0);
2205022050
checkAccessorDeclarationTypesIdentical(node, otherAccessor, getThisTypeOfDeclaration, Diagnostics.get_and_set_accessor_must_have_the_same_this_type);
2205122051
}
2205222052
}
@@ -22062,11 +22062,14 @@ namespace ts {
2206222062
const firstType = getAnnotatedType(first);
2206322063
const secondType = getAnnotatedType(second);
2206422064
if (firstType && secondType && !isTypeIdenticalTo(firstType, secondType)) {
22065-
const typeNameFirstType = typeToString(firstType);
2206622065
const typeNameSecondType = typeToString(secondType);
2206722066

22068-
const diagnostic: Diagnostic = error(first, message, typeNameSecondType, typeNameFirstType);
22069-
addRelatedInfo(diagnostic, createDiagnosticForNode(first, Diagnostics.The_respective_set_accessor_has_the_type_0, typeNameSecondType));
22067+
if(isGetAccessor(first)){
22068+
22069+
const diagnostic: Diagnostic = error(first, message, typeNameSecondType);
22070+
addRelatedInfo(diagnostic, createDiagnosticForNode(first, Diagnostics.The_respective_set_accessor_has_the_type_0, typeNameSecondType));
22071+
22072+
}
2207022073
}
2207122074
}
2207222075

src/compiler/diagnosticMessages.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1304,7 +1304,7 @@
13041304
"category": "Error",
13051305
"code": 2379
13061306
},
1307-
"'get' and 'set' accessor must have the same type '{0}', but this 'get' accessor has the type '{1}'.": {
1307+
"'get' and 'set' accessor must have the same type '{0}'.": {
13081308
"category": "Error",
13091309
"code": 2380
13101310
},

tests/baselines/reference/abstractPropertyNegative.errors.txt

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
tests/cases/compiler/abstractPropertyNegative.ts(10,18): error TS2380: 'get' and 'set' accessor must have the same type 'number', but this 'get' accessor has the type 'string'.
2-
tests/cases/compiler/abstractPropertyNegative.ts(11,18): error TS2380: 'get' and 'set' accessor must have the same type 'string', but this 'get' accessor has the type 'number'.
1+
tests/cases/compiler/abstractPropertyNegative.ts(10,18): error TS2380: 'get' and 'set' accessor must have the same type 'number'.
32
tests/cases/compiler/abstractPropertyNegative.ts(13,7): error TS2515: Non-abstract class 'C' does not implement inherited abstract member 'm' from class 'B'.
43
tests/cases/compiler/abstractPropertyNegative.ts(13,7): error TS2515: Non-abstract class 'C' does not implement inherited abstract member 'mismatch' from class 'B'.
54
tests/cases/compiler/abstractPropertyNegative.ts(13,7): error TS2515: Non-abstract class 'C' does not implement inherited abstract member 'prop' from class 'B'.
@@ -19,7 +18,7 @@ tests/cases/compiler/abstractPropertyNegative.ts(40,9): error TS2676: Accessors
1918
tests/cases/compiler/abstractPropertyNegative.ts(41,18): error TS2676: Accessors must both be abstract or non-abstract.
2019

2120

22-
==== tests/cases/compiler/abstractPropertyNegative.ts (16 errors) ====
21+
==== tests/cases/compiler/abstractPropertyNegative.ts (15 errors) ====
2322
interface A {
2423
prop: string;
2524
m(): string;
@@ -31,12 +30,9 @@ tests/cases/compiler/abstractPropertyNegative.ts(41,18): error TS2676: Accessors
3130
abstract m(): string;
3231
abstract get mismatch(): string;
3332
~~~~~~~~
34-
!!! error TS2380: 'get' and 'set' accessor must have the same type 'number', but this 'get' accessor has the type 'string'.
33+
!!! error TS2380: 'get' and 'set' accessor must have the same type 'number'.
3534
!!! related TS2730 tests/cases/compiler/abstractPropertyNegative.ts:10:18: The respective 'set' accessor has the type 'number'.
3635
abstract set mismatch(val: number); // error, not same type
37-
~~~~~~~~
38-
!!! error TS2380: 'get' and 'set' accessor must have the same type 'string', but this 'get' accessor has the type 'number'.
39-
!!! related TS2730 tests/cases/compiler/abstractPropertyNegative.ts:11:18: The respective 'set' accessor has the type 'string'.
4036
}
4137
class C extends B {
4238
~

tests/baselines/reference/api/tsserverlibrary.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5156,7 +5156,7 @@ declare namespace ts {
51565156
Constructors_for_derived_classes_must_contain_a_super_call: DiagnosticMessage;
51575157
A_get_accessor_must_return_a_value: DiagnosticMessage;
51585158
Getter_and_setter_accessors_do_not_agree_in_visibility: DiagnosticMessage;
5159-
get_and_set_accessor_must_have_the_same_type_0_but_this_get_accessor_has_the_type_1: DiagnosticMessage;
5159+
get_and_set_accessor_must_have_the_same_type_0: DiagnosticMessage;
51605160
A_signature_with_an_implementation_cannot_use_a_string_literal_type: DiagnosticMessage;
51615161
Specialized_overload_signature_is_not_assignable_to_any_non_specialized_signature: DiagnosticMessage;
51625162
Overload_signatures_must_all_be_exported_or_non_exported: DiagnosticMessage;
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
tests/cases/compiler/getAndSetNotIdenticalType.ts(2,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
2-
tests/cases/compiler/getAndSetNotIdenticalType.ts(2,9): error TS2380: 'get' and 'set' accessor must have the same type 'string', but this 'get' accessor has the type 'number'.
2+
tests/cases/compiler/getAndSetNotIdenticalType.ts(2,9): error TS2380: 'get' and 'set' accessor must have the same type 'string'.
33
tests/cases/compiler/getAndSetNotIdenticalType.ts(5,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
4-
tests/cases/compiler/getAndSetNotIdenticalType.ts(5,9): error TS2380: 'get' and 'set' accessor must have the same type 'number', but this 'get' accessor has the type 'string'.
54

65

7-
==== tests/cases/compiler/getAndSetNotIdenticalType.ts (4 errors) ====
6+
==== tests/cases/compiler/getAndSetNotIdenticalType.ts (3 errors) ====
87
class C {
98
get x(): number {
109
~
1110
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
1211
~
13-
!!! error TS2380: 'get' and 'set' accessor must have the same type 'string', but this 'get' accessor has the type 'number'.
12+
!!! error TS2380: 'get' and 'set' accessor must have the same type 'string'.
1413
!!! related TS2730 tests/cases/compiler/getAndSetNotIdenticalType.ts:2:9: The respective 'set' accessor has the type 'string'.
1514
return 1;
1615
}
1716
set x(v: string) { }
1817
~
1918
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
20-
~
21-
!!! error TS2380: 'get' and 'set' accessor must have the same type 'number', but this 'get' accessor has the type 'string'.
22-
!!! related TS2730 tests/cases/compiler/getAndSetNotIdenticalType.ts:5:9: The respective 'set' accessor has the type 'number'.
2319
}

tests/baselines/reference/getAndSetNotIdenticalType2.errors.txt

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
tests/cases/compiler/getAndSetNotIdenticalType2.ts(5,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
2-
tests/cases/compiler/getAndSetNotIdenticalType2.ts(5,9): error TS2380: 'get' and 'set' accessor must have the same type 'A<string>', but this 'get' accessor has the type 'A<T>'.
2+
tests/cases/compiler/getAndSetNotIdenticalType2.ts(5,9): error TS2380: 'get' and 'set' accessor must have the same type 'A<string>'.
33
tests/cases/compiler/getAndSetNotIdenticalType2.ts(8,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
4-
tests/cases/compiler/getAndSetNotIdenticalType2.ts(8,9): error TS2380: 'get' and 'set' accessor must have the same type 'A<T>', but this 'get' accessor has the type 'A<string>'.
54
tests/cases/compiler/getAndSetNotIdenticalType2.ts(9,9): error TS2322: Type 'A<string>' is not assignable to type 'A<T>'.
65
Type 'string' is not assignable to type 'T'.
76

87

9-
==== tests/cases/compiler/getAndSetNotIdenticalType2.ts (5 errors) ====
8+
==== tests/cases/compiler/getAndSetNotIdenticalType2.ts (4 errors) ====
109
class A<T> { foo: T; }
1110

1211
class C<T> {
@@ -15,16 +14,13 @@ tests/cases/compiler/getAndSetNotIdenticalType2.ts(9,9): error TS2322: Type 'A<s
1514
~
1615
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
1716
~
18-
!!! error TS2380: 'get' and 'set' accessor must have the same type 'A<string>', but this 'get' accessor has the type 'A<T>'.
17+
!!! error TS2380: 'get' and 'set' accessor must have the same type 'A<string>'.
1918
!!! related TS2730 tests/cases/compiler/getAndSetNotIdenticalType2.ts:5:9: The respective 'set' accessor has the type 'A<string>'.
2019
return this.data;
2120
}
2221
set x(v: A<string>) {
2322
~
2423
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
25-
~
26-
!!! error TS2380: 'get' and 'set' accessor must have the same type 'A<T>', but this 'get' accessor has the type 'A<string>'.
27-
!!! related TS2730 tests/cases/compiler/getAndSetNotIdenticalType2.ts:8:9: The respective 'set' accessor has the type 'A<T>'.
2824
this.data = v;
2925
~~~~~~~~~
3026
!!! error TS2322: Type 'A<string>' is not assignable to type 'A<T>'.

tests/baselines/reference/getAndSetNotIdenticalType3.errors.txt

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
tests/cases/compiler/getAndSetNotIdenticalType3.ts(5,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
2-
tests/cases/compiler/getAndSetNotIdenticalType3.ts(5,9): error TS2380: 'get' and 'set' accessor must have the same type 'A<string>', but this 'get' accessor has the type 'A<number>'.
2+
tests/cases/compiler/getAndSetNotIdenticalType3.ts(5,9): error TS2380: 'get' and 'set' accessor must have the same type 'A<string>'.
33
tests/cases/compiler/getAndSetNotIdenticalType3.ts(8,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
4-
tests/cases/compiler/getAndSetNotIdenticalType3.ts(8,9): error TS2380: 'get' and 'set' accessor must have the same type 'A<number>', but this 'get' accessor has the type 'A<string>'.
54
tests/cases/compiler/getAndSetNotIdenticalType3.ts(9,9): error TS2322: Type 'A<string>' is not assignable to type 'A<number>'.
65
Type 'string' is not assignable to type 'number'.
76

87

9-
==== tests/cases/compiler/getAndSetNotIdenticalType3.ts (5 errors) ====
8+
==== tests/cases/compiler/getAndSetNotIdenticalType3.ts (4 errors) ====
109
class A<T> { foo: T; }
1110

1211
class C<T> {
@@ -15,16 +14,13 @@ tests/cases/compiler/getAndSetNotIdenticalType3.ts(9,9): error TS2322: Type 'A<s
1514
~
1615
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
1716
~
18-
!!! error TS2380: 'get' and 'set' accessor must have the same type 'A<string>', but this 'get' accessor has the type 'A<number>'.
17+
!!! error TS2380: 'get' and 'set' accessor must have the same type 'A<string>'.
1918
!!! related TS2730 tests/cases/compiler/getAndSetNotIdenticalType3.ts:5:9: The respective 'set' accessor has the type 'A<string>'.
2019
return this.data;
2120
}
2221
set x(v: A<string>) {
2322
~
2423
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
25-
~
26-
!!! error TS2380: 'get' and 'set' accessor must have the same type 'A<number>', but this 'get' accessor has the type 'A<string>'.
27-
!!! related TS2730 tests/cases/compiler/getAndSetNotIdenticalType3.ts:8:9: The respective 'set' accessor has the type 'A<number>'.
2824
this.data = v;
2925
~~~~~~~~~
3026
!!! error TS2322: Type 'A<string>' is not assignable to type 'A<number>'.

tests/baselines/reference/objectLiteralErrors.errors.txt

+5-13
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,12 @@ tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(38,26)
7171
tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(39,13): error TS2300: Duplicate identifier 'a'.
7272
tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(39,46): error TS1119: An object literal cannot have property and accessor with the same name.
7373
tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(39,46): error TS2300: Duplicate identifier 'a'.
74-
tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(42,16): error TS2380: 'get' and 'set' accessor must have the same type 'string', but this 'get' accessor has the type 'number'.
75-
tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(42,47): error TS2380: 'get' and 'set' accessor must have the same type 'number', but this 'get' accessor has the type 'string'.
74+
tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(42,16): error TS2380: 'get' and 'set' accessor must have the same type 'string'.
7675
tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(43,22): error TS2322: Type '4' is not assignable to type 'string'.
77-
tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(44,16): error TS2380: 'get' and 'set' accessor must have the same type 'string', but this 'get' accessor has the type 'number'.
78-
tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(44,55): error TS2380: 'get' and 'set' accessor must have the same type 'number', but this 'get' accessor has the type 'string'.
76+
tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(44,16): error TS2380: 'get' and 'set' accessor must have the same type 'string'.
7977

8078

81-
==== tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts (78 errors) ====
79+
==== tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts (76 errors) ====
8280
// Multiple properties with the same name
8381
var e1 = { a: 0, a: 0 };
8482
~
@@ -268,19 +266,13 @@ tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(44,55)
268266
// Get and set accessor with mismatched type annotations
269267
var g1 = { get a(): number { return 4; }, set a(n: string) { } };
270268
~
271-
!!! error TS2380: 'get' and 'set' accessor must have the same type 'string', but this 'get' accessor has the type 'number'.
269+
!!! error TS2380: 'get' and 'set' accessor must have the same type 'string'.
272270
!!! related TS2730 tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts:42:16: The respective 'set' accessor has the type 'string'.
273-
~
274-
!!! error TS2380: 'get' and 'set' accessor must have the same type 'number', but this 'get' accessor has the type 'string'.
275-
!!! related TS2730 tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts:42:47: The respective 'set' accessor has the type 'number'.
276271
var g2 = { get a() { return 4; }, set a(n: string) { } };
277272
~~~~~~~~~
278273
!!! error TS2322: Type '4' is not assignable to type 'string'.
279274
var g3 = { get a(): number { return undefined; }, set a(n: string) { } };
280275
~
281-
!!! error TS2380: 'get' and 'set' accessor must have the same type 'string', but this 'get' accessor has the type 'number'.
276+
!!! error TS2380: 'get' and 'set' accessor must have the same type 'string'.
282277
!!! related TS2730 tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts:44:16: The respective 'set' accessor has the type 'string'.
283-
~
284-
!!! error TS2380: 'get' and 'set' accessor must have the same type 'number', but this 'get' accessor has the type 'string'.
285-
!!! related TS2730 tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts:44:55: The respective 'set' accessor has the type 'number'.
286278

tests/baselines/reference/thisTypeInAccessorsNegative.errors.txt

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts(10,9): error TS2682: 'get' and 'set' accessor must have the same 'this' type.
2-
tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts(11,9): error TS2682: 'get' and 'set' accessor must have the same 'this' type.
32

43

5-
==== tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts (2 errors) ====
4+
==== tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts (1 errors) ====
65
interface Foo {
76
n: number;
87
x: number;
@@ -17,9 +16,6 @@ tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts(11,9): err
1716
!!! error TS2682: 'get' and 'set' accessor must have the same 'this' type.
1817
!!! related TS2730 tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts:10:9: The respective 'set' accessor has the type 'Bar'.
1918
set x(this: Bar, n) { this.wrong = "method"; }
20-
~
21-
!!! error TS2682: 'get' and 'set' accessor must have the same 'this' type.
22-
!!! related TS2730 tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts:11:9: The respective 'set' accessor has the type 'Foo'.
2319
}
2420
const contextual: Foo = {
2521
n: 16,

0 commit comments

Comments
 (0)