diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 9128398299c6c..6dad58ebe0249 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6824,9 +6824,15 @@ namespace ts { } if (!message) { - message = relation === comparableRelation ? - Diagnostics.Type_0_is_not_comparable_to_type_1 : - Diagnostics.Type_0_is_not_assignable_to_type_1; + if (relation === comparableRelation) { + message = Diagnostics.Type_0_is_not_comparable_to_type_1; + } + else if (sourceType === targetType) { + message = Diagnostics.Type_0_is_not_assignable_to_type_1_Two_different_types_with_this_name_exist_but_they_are_unrelated; + } + else { + message = Diagnostics.Type_0_is_not_assignable_to_type_1; + } } reportError(message, sourceType, targetType); diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 5bcc255c0a5ca..bc2ab6c3ff470 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -3170,5 +3170,9 @@ "Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig": { "category": "Error", "code": 90009 + }, + "Type '{0}' is not assignable to type '{1}'. Two different types with this name exist, but they are unrelated.": { + "category": "Error", + "code": 90010 } } diff --git a/tests/baselines/reference/incompatibleAssignmentOfIdenticallyNamedTypes.errors.txt b/tests/baselines/reference/incompatibleAssignmentOfIdenticallyNamedTypes.errors.txt new file mode 100644 index 0000000000000..3f1de47c3476d --- /dev/null +++ b/tests/baselines/reference/incompatibleAssignmentOfIdenticallyNamedTypes.errors.txt @@ -0,0 +1,15 @@ +tests/cases/compiler/incompatibleAssignmentOfIdenticallyNamedTypes.ts(6,9): error TS90010: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. + + +==== tests/cases/compiler/incompatibleAssignmentOfIdenticallyNamedTypes.ts (1 errors) ==== + interface T { } + declare const a: T; + class Foo { + x: T; + fn() { + this.x = a; + ~~~~~~ +!!! error TS90010: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. + } + } + \ No newline at end of file diff --git a/tests/baselines/reference/incompatibleAssignmentOfIdenticallyNamedTypes.js b/tests/baselines/reference/incompatibleAssignmentOfIdenticallyNamedTypes.js new file mode 100644 index 0000000000000..677d177a4d01a --- /dev/null +++ b/tests/baselines/reference/incompatibleAssignmentOfIdenticallyNamedTypes.js @@ -0,0 +1,20 @@ +//// [incompatibleAssignmentOfIdenticallyNamedTypes.ts] +interface T { } +declare const a: T; +class Foo { + x: T; + fn() { + this.x = a; + } +} + + +//// [incompatibleAssignmentOfIdenticallyNamedTypes.js] +var Foo = (function () { + function Foo() { + } + Foo.prototype.fn = function () { + this.x = a; + }; + return Foo; +}()); diff --git a/tests/cases/compiler/incompatibleAssignmentOfIdenticallyNamedTypes.ts b/tests/cases/compiler/incompatibleAssignmentOfIdenticallyNamedTypes.ts new file mode 100644 index 0000000000000..493feef1fd724 --- /dev/null +++ b/tests/cases/compiler/incompatibleAssignmentOfIdenticallyNamedTypes.ts @@ -0,0 +1,8 @@ +interface T { } +declare const a: T; +class Foo { + x: T; + fn() { + this.x = a; + } +}