Skip to content

Commit c05e226

Browse files
committed
Adds error message for incompatible assignment of identically named type
Fixes issue microsoft#12050
1 parent be5e5fb commit c05e226

File tree

5 files changed

+56
-3
lines changed

5 files changed

+56
-3
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6824,9 +6824,15 @@ namespace ts {
68246824
}
68256825

68266826
if (!message) {
6827-
message = relation === comparableRelation ?
6828-
Diagnostics.Type_0_is_not_comparable_to_type_1 :
6829-
Diagnostics.Type_0_is_not_assignable_to_type_1;
6827+
if (relation === comparableRelation) {
6828+
message = Diagnostics.Type_0_is_not_comparable_to_type_1;
6829+
}
6830+
else if (sourceType === targetType) {
6831+
message = Diagnostics.Type_0_is_not_assignable_to_type_1_Two_different_types_with_this_name_exist_but_they_are_unrelated;
6832+
}
6833+
else {
6834+
message = Diagnostics.Type_0_is_not_assignable_to_type_1;
6835+
}
68306836
}
68316837

68326838
reportError(message, sourceType, targetType);

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3170,5 +3170,9 @@
31703170
"Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig": {
31713171
"category": "Error",
31723172
"code": 90009
3173+
},
3174+
"Type '{0}' is not assignable to type '{1}'. Two different types with this name exist, but they are unrelated.": {
3175+
"category": "Error",
3176+
"code": 90010
31733177
}
31743178
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
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.
2+
3+
4+
==== tests/cases/compiler/incompatibleAssignmentOfIdenticallyNamedTypes.ts (1 errors) ====
5+
interface T { }
6+
declare const a: T;
7+
class Foo<T> {
8+
x: T;
9+
fn() {
10+
this.x = a;
11+
~~~~~~
12+
!!! error TS90010: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated.
13+
}
14+
}
15+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//// [incompatibleAssignmentOfIdenticallyNamedTypes.ts]
2+
interface T { }
3+
declare const a: T;
4+
class Foo<T> {
5+
x: T;
6+
fn() {
7+
this.x = a;
8+
}
9+
}
10+
11+
12+
//// [incompatibleAssignmentOfIdenticallyNamedTypes.js]
13+
var Foo = (function () {
14+
function Foo() {
15+
}
16+
Foo.prototype.fn = function () {
17+
this.x = a;
18+
};
19+
return Foo;
20+
}());
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
interface T { }
2+
declare const a: T;
3+
class Foo<T> {
4+
x: T;
5+
fn() {
6+
this.x = a;
7+
}
8+
}

0 commit comments

Comments
 (0)