Skip to content

Commit 0976754

Browse files
committed
Adds error messages for incompatible assignment of identically named type
Fixes issue #12050
1 parent 28cc938 commit 0976754

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
@@ -6730,9 +6730,15 @@ namespace ts {
67306730
}
67316731

67326732
if (!message) {
6733-
message = relation === comparableRelation ?
6734-
Diagnostics.Type_0_is_not_comparable_to_type_1 :
6735-
Diagnostics.Type_0_is_not_assignable_to_type_1;
6733+
if (relation === comparableRelation) {
6734+
message = Diagnostics.Type_0_is_not_comparable_to_type_1;
6735+
}
6736+
else if (sourceType === targetType) {
6737+
message = Diagnostics.Type_0_is_not_assignable_to_type_1_Two_different_types_with_this_name_exist_but_they_are_unrelated;
6738+
}
6739+
else {
6740+
message = Diagnostics.Type_0_is_not_assignable_to_type_1;
6741+
}
67366742
}
67376743

67386744
reportError(message, sourceType, targetType);

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3162,5 +3162,9 @@
31623162
"Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig": {
31633163
"category": "Error",
31643164
"code": 90009
3165+
},
3166+
"Type '{0}' is not assignable to type '{1}'. Two different types with this name exist, but they are unrelated.": {
3167+
"category": "Error",
3168+
"code": 90010
31653169
}
31663170
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
tests/cases/compiler/incompatableAssignementOfIdenticallyNamedTypes.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/incompatableAssignementOfIdenticallyNamedTypes.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+
//// [incompatableAssignementOfIdenticallyNamedTypes.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+
//// [incompatableAssignementOfIdenticallyNamedTypes.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)