Skip to content

Commit 6e1aac1

Browse files
authored
Merge pull request #12147 from mylesmegyesi/12050-IncompatableAssignementOfIdenticallyNamedTypes
Adds error message for incompatible assignment of identically named type
2 parents 9a8e1bf + c05e226 commit 6e1aac1

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
@@ -6843,9 +6843,15 @@ namespace ts {
68436843
}
68446844

68456845
if (!message) {
6846-
message = relation === comparableRelation ?
6847-
Diagnostics.Type_0_is_not_comparable_to_type_1 :
6848-
Diagnostics.Type_0_is_not_assignable_to_type_1;
6846+
if (relation === comparableRelation) {
6847+
message = Diagnostics.Type_0_is_not_comparable_to_type_1;
6848+
}
6849+
else if (sourceType === targetType) {
6850+
message = Diagnostics.Type_0_is_not_assignable_to_type_1_Two_different_types_with_this_name_exist_but_they_are_unrelated;
6851+
}
6852+
else {
6853+
message = Diagnostics.Type_0_is_not_assignable_to_type_1;
6854+
}
68496855
}
68506856

68516857
reportError(message, sourceType, targetType);

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3178,5 +3178,9 @@
31783178
"Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig": {
31793179
"category": "Error",
31803180
"code": 90009
3181+
},
3182+
"Type '{0}' is not assignable to type '{1}'. Two different types with this name exist, but they are unrelated.": {
3183+
"category": "Error",
3184+
"code": 90010
31813185
}
31823186
}
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)