Skip to content

Adds error message for incompatible assignment of identically named type #12147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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<T> {
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.
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//// [incompatibleAssignmentOfIdenticallyNamedTypes.ts]
interface T { }
declare const a: T;
class Foo<T> {
x: T;
fn() {
this.x = a;
}
}


//// [incompatibleAssignmentOfIdenticallyNamedTypes.js]
var Foo = (function () {
function Foo() {
}
Foo.prototype.fn = function () {
this.x = a;
};
return Foo;
}());
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
interface T { }
declare const a: T;
class Foo<T> {
x: T;
fn() {
this.x = a;
}
}