diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 207d731707878..aefe71c97871f 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -22046,7 +22046,7 @@ namespace ts { // TypeScript 1.0 spec (April 2014): 4.5 // If both accessors include type annotations, the specified types must be identical. - checkAccessorDeclarationTypesIdentical(node, otherAccessor, getAnnotatedAccessorType, Diagnostics.get_and_set_accessor_must_have_the_same_type); + checkAccessorDeclarationTypesIdentical(node, otherAccessor, getAnnotatedAccessorType, Diagnostics.get_and_set_accessor_must_have_the_same_type_0); checkAccessorDeclarationTypesIdentical(node, otherAccessor, getThisTypeOfDeclaration, Diagnostics.get_and_set_accessor_must_have_the_same_this_type); } } @@ -22062,7 +22062,11 @@ namespace ts { const firstType = getAnnotatedType(first); const secondType = getAnnotatedType(second); if (firstType && secondType && !isTypeIdenticalTo(firstType, secondType)) { - error(first, message); + const typeNameSecondType = typeToString(secondType); + if(isGetAccessor(first)){ + const diagnostic: Diagnostic = error(first, message, typeNameSecondType); + addRelatedInfo(diagnostic, createDiagnosticForNode(first, Diagnostics.The_respective_set_accessor_has_the_type_0, typeNameSecondType)); + } } } diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 40a4c4d09d402..415988577dc68 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1304,7 +1304,7 @@ "category": "Error", "code": 2379 }, - "'get' and 'set' accessor must have the same type.": { + "'get' and 'set' accessor must have the same type '{0}'.": { "category": "Error", "code": 2380 }, @@ -2409,6 +2409,10 @@ "category": "Error", "code": 2729 }, + "The respective 'set' accessor has the type '{0}'.": { + "category": "Message", + "code": 2730 + }, "Import declaration '{0}' is using private name '{1}'.": { "category": "Error", diff --git a/tests/baselines/reference/abstractPropertyNegative.errors.txt b/tests/baselines/reference/abstractPropertyNegative.errors.txt index be1c85374fb92..d2046652d4577 100644 --- a/tests/baselines/reference/abstractPropertyNegative.errors.txt +++ b/tests/baselines/reference/abstractPropertyNegative.errors.txt @@ -1,5 +1,4 @@ -tests/cases/compiler/abstractPropertyNegative.ts(10,18): error TS2380: 'get' and 'set' accessor must have the same type. -tests/cases/compiler/abstractPropertyNegative.ts(11,18): error TS2380: 'get' and 'set' accessor must have the same type. +tests/cases/compiler/abstractPropertyNegative.ts(10,18): error TS2380: 'get' and 'set' accessor must have the same type 'number'. tests/cases/compiler/abstractPropertyNegative.ts(13,7): error TS2515: Non-abstract class 'C' does not implement inherited abstract member 'm' from class 'B'. tests/cases/compiler/abstractPropertyNegative.ts(13,7): error TS2515: Non-abstract class 'C' does not implement inherited abstract member 'mismatch' from class 'B'. 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 tests/cases/compiler/abstractPropertyNegative.ts(41,18): error TS2676: Accessors must both be abstract or non-abstract. -==== tests/cases/compiler/abstractPropertyNegative.ts (16 errors) ==== +==== tests/cases/compiler/abstractPropertyNegative.ts (15 errors) ==== interface A { prop: string; m(): string; @@ -31,10 +30,9 @@ tests/cases/compiler/abstractPropertyNegative.ts(41,18): error TS2676: Accessors abstract m(): string; abstract get mismatch(): string; ~~~~~~~~ -!!! error TS2380: 'get' and 'set' accessor must have the same type. +!!! error TS2380: 'get' and 'set' accessor must have the same type 'number'. +!!! related TS2730 tests/cases/compiler/abstractPropertyNegative.ts:10:18: The respective 'set' accessor has the type 'number'. abstract set mismatch(val: number); // error, not same type - ~~~~~~~~ -!!! error TS2380: 'get' and 'set' accessor must have the same type. } class C extends B { ~ diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 99452b93d4763..514311b110b8e 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -5156,7 +5156,7 @@ declare namespace ts { Constructors_for_derived_classes_must_contain_a_super_call: DiagnosticMessage; A_get_accessor_must_return_a_value: DiagnosticMessage; Getter_and_setter_accessors_do_not_agree_in_visibility: DiagnosticMessage; - get_and_set_accessor_must_have_the_same_type: DiagnosticMessage; + get_and_set_accessor_must_have_the_same_type_0: DiagnosticMessage; A_signature_with_an_implementation_cannot_use_a_string_literal_type: DiagnosticMessage; Specialized_overload_signature_is_not_assignable_to_any_non_specialized_signature: DiagnosticMessage; Overload_signatures_must_all_be_exported_or_non_exported: DiagnosticMessage; @@ -5432,6 +5432,7 @@ declare namespace ts { Cannot_find_lib_definition_for_0_Did_you_mean_1: DiagnosticMessage; _0_was_declared_here: DiagnosticMessage; Property_0_is_used_before_its_initialization: DiagnosticMessage; + The_respective_set_accessor_has_the_type_0: DiagnosticMessage; Import_declaration_0_is_using_private_name_1: DiagnosticMessage; Type_parameter_0_of_exported_class_has_or_is_using_private_name_1: DiagnosticMessage; Type_parameter_0_of_exported_interface_has_or_is_using_private_name_1: DiagnosticMessage; diff --git a/tests/baselines/reference/getAndSetNotIdenticalType.errors.txt b/tests/baselines/reference/getAndSetNotIdenticalType.errors.txt index a16cbfe7b37a5..dc72501342d40 100644 --- a/tests/baselines/reference/getAndSetNotIdenticalType.errors.txt +++ b/tests/baselines/reference/getAndSetNotIdenticalType.errors.txt @@ -1,21 +1,19 @@ tests/cases/compiler/getAndSetNotIdenticalType.ts(2,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. -tests/cases/compiler/getAndSetNotIdenticalType.ts(2,9): error TS2380: 'get' and 'set' accessor must have the same type. +tests/cases/compiler/getAndSetNotIdenticalType.ts(2,9): error TS2380: 'get' and 'set' accessor must have the same type 'string'. tests/cases/compiler/getAndSetNotIdenticalType.ts(5,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. -tests/cases/compiler/getAndSetNotIdenticalType.ts(5,9): error TS2380: 'get' and 'set' accessor must have the same type. -==== tests/cases/compiler/getAndSetNotIdenticalType.ts (4 errors) ==== +==== tests/cases/compiler/getAndSetNotIdenticalType.ts (3 errors) ==== class C { get x(): number { ~ !!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. ~ -!!! error TS2380: 'get' and 'set' accessor must have the same type. +!!! error TS2380: 'get' and 'set' accessor must have the same type 'string'. +!!! related TS2730 tests/cases/compiler/getAndSetNotIdenticalType.ts:2:9: The respective 'set' accessor has the type 'string'. return 1; } set x(v: string) { } ~ !!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. - ~ -!!! error TS2380: 'get' and 'set' accessor must have the same type. } \ No newline at end of file diff --git a/tests/baselines/reference/getAndSetNotIdenticalType2.errors.txt b/tests/baselines/reference/getAndSetNotIdenticalType2.errors.txt index c9fae8cc89c4a..2acc0538d40e1 100644 --- a/tests/baselines/reference/getAndSetNotIdenticalType2.errors.txt +++ b/tests/baselines/reference/getAndSetNotIdenticalType2.errors.txt @@ -1,12 +1,11 @@ tests/cases/compiler/getAndSetNotIdenticalType2.ts(5,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. -tests/cases/compiler/getAndSetNotIdenticalType2.ts(5,9): error TS2380: 'get' and 'set' accessor must have the same type. +tests/cases/compiler/getAndSetNotIdenticalType2.ts(5,9): error TS2380: 'get' and 'set' accessor must have the same type 'A'. tests/cases/compiler/getAndSetNotIdenticalType2.ts(8,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. -tests/cases/compiler/getAndSetNotIdenticalType2.ts(8,9): error TS2380: 'get' and 'set' accessor must have the same type. tests/cases/compiler/getAndSetNotIdenticalType2.ts(9,9): error TS2322: Type 'A' is not assignable to type 'A'. Type 'string' is not assignable to type 'T'. -==== tests/cases/compiler/getAndSetNotIdenticalType2.ts (5 errors) ==== +==== tests/cases/compiler/getAndSetNotIdenticalType2.ts (4 errors) ==== class A { foo: T; } class C { @@ -15,14 +14,13 @@ tests/cases/compiler/getAndSetNotIdenticalType2.ts(9,9): error TS2322: Type 'A'. +!!! related TS2730 tests/cases/compiler/getAndSetNotIdenticalType2.ts:5:9: The respective 'set' accessor has the type 'A'. return this.data; } set x(v: A) { ~ !!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. - ~ -!!! error TS2380: 'get' and 'set' accessor must have the same type. this.data = v; ~~~~~~~~~ !!! error TS2322: Type 'A' is not assignable to type 'A'. diff --git a/tests/baselines/reference/getAndSetNotIdenticalType3.errors.txt b/tests/baselines/reference/getAndSetNotIdenticalType3.errors.txt index 3fb17d65a400b..75bb83fa8c0ac 100644 --- a/tests/baselines/reference/getAndSetNotIdenticalType3.errors.txt +++ b/tests/baselines/reference/getAndSetNotIdenticalType3.errors.txt @@ -1,12 +1,11 @@ tests/cases/compiler/getAndSetNotIdenticalType3.ts(5,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. -tests/cases/compiler/getAndSetNotIdenticalType3.ts(5,9): error TS2380: 'get' and 'set' accessor must have the same type. +tests/cases/compiler/getAndSetNotIdenticalType3.ts(5,9): error TS2380: 'get' and 'set' accessor must have the same type 'A'. tests/cases/compiler/getAndSetNotIdenticalType3.ts(8,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. -tests/cases/compiler/getAndSetNotIdenticalType3.ts(8,9): error TS2380: 'get' and 'set' accessor must have the same type. tests/cases/compiler/getAndSetNotIdenticalType3.ts(9,9): error TS2322: Type 'A' is not assignable to type 'A'. Type 'string' is not assignable to type 'number'. -==== tests/cases/compiler/getAndSetNotIdenticalType3.ts (5 errors) ==== +==== tests/cases/compiler/getAndSetNotIdenticalType3.ts (4 errors) ==== class A { foo: T; } class C { @@ -15,14 +14,13 @@ tests/cases/compiler/getAndSetNotIdenticalType3.ts(9,9): error TS2322: Type 'A'. +!!! related TS2730 tests/cases/compiler/getAndSetNotIdenticalType3.ts:5:9: The respective 'set' accessor has the type 'A'. return this.data; } set x(v: A) { ~ !!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. - ~ -!!! error TS2380: 'get' and 'set' accessor must have the same type. this.data = v; ~~~~~~~~~ !!! error TS2322: Type 'A' is not assignable to type 'A'. diff --git a/tests/baselines/reference/objectLiteralErrors.errors.txt b/tests/baselines/reference/objectLiteralErrors.errors.txt index 2a2dc7dd5753c..3987a8dc003de 100644 --- a/tests/baselines/reference/objectLiteralErrors.errors.txt +++ b/tests/baselines/reference/objectLiteralErrors.errors.txt @@ -71,14 +71,12 @@ tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(38,26) tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(39,13): error TS2300: Duplicate identifier 'a'. tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(39,46): error TS1119: An object literal cannot have property and accessor with the same name. tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(39,46): error TS2300: Duplicate identifier 'a'. -tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(42,16): error TS2380: 'get' and 'set' accessor must have the same type. -tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(42,47): error TS2380: 'get' and 'set' accessor must have the same type. +tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(42,16): error TS2380: 'get' and 'set' accessor must have the same type 'string'. tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(43,22): error TS2322: Type '4' is not assignable to type 'string'. -tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(44,16): error TS2380: 'get' and 'set' accessor must have the same type. -tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(44,55): error TS2380: 'get' and 'set' accessor must have the same type. +tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(44,16): error TS2380: 'get' and 'set' accessor must have the same type 'string'. -==== tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts (78 errors) ==== +==== tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts (76 errors) ==== // Multiple properties with the same name var e1 = { a: 0, a: 0 }; ~ @@ -268,15 +266,13 @@ tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(44,55) // Get and set accessor with mismatched type annotations var g1 = { get a(): number { return 4; }, set a(n: string) { } }; ~ -!!! error TS2380: 'get' and 'set' accessor must have the same type. - ~ -!!! error TS2380: 'get' and 'set' accessor must have the same type. +!!! error TS2380: 'get' and 'set' accessor must have the same type 'string'. +!!! related TS2730 tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts:42:16: The respective 'set' accessor has the type 'string'. var g2 = { get a() { return 4; }, set a(n: string) { } }; ~~~~~~~~~ !!! error TS2322: Type '4' is not assignable to type 'string'. var g3 = { get a(): number { return undefined; }, set a(n: string) { } }; ~ -!!! error TS2380: 'get' and 'set' accessor must have the same type. - ~ -!!! error TS2380: 'get' and 'set' accessor must have the same type. +!!! error TS2380: 'get' and 'set' accessor must have the same type 'string'. +!!! related TS2730 tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts:44:16: The respective 'set' accessor has the type 'string'. \ No newline at end of file diff --git a/tests/baselines/reference/thisTypeInAccessorsNegative.errors.txt b/tests/baselines/reference/thisTypeInAccessorsNegative.errors.txt index cd60967372353..e6f09020db19d 100644 --- a/tests/baselines/reference/thisTypeInAccessorsNegative.errors.txt +++ b/tests/baselines/reference/thisTypeInAccessorsNegative.errors.txt @@ -1,8 +1,7 @@ tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts(10,9): error TS2682: 'get' and 'set' accessor must have the same 'this' type. -tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts(11,9): error TS2682: 'get' and 'set' accessor must have the same 'this' type. -==== tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts (2 errors) ==== +==== tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts (1 errors) ==== interface Foo { n: number; x: number; @@ -15,9 +14,8 @@ tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts(11,9): err get x(this: Foo) { return this.n; }, ~ !!! error TS2682: 'get' and 'set' accessor must have the same 'this' type. +!!! related TS2730 tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts:10:9: The respective 'set' accessor has the type 'Bar'. set x(this: Bar, n) { this.wrong = "method"; } - ~ -!!! error TS2682: 'get' and 'set' accessor must have the same 'this' type. } const contextual: Foo = { n: 16,