Adds error message for incompatible assignment of identically named type

Fixes issue #12050
This commit is contained in:
Myles Megyesi
2016-11-10 11:31:27 -06:00
parent be5e5fb872
commit c05e226c4b
5 changed files with 56 additions and 3 deletions

View File

@@ -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);

View File

@@ -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
}
}

View File

@@ -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.
}
}

View File

@@ -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;
}());

View File

@@ -0,0 +1,8 @@
interface T { }
declare const a: T;
class Foo<T> {
x: T;
fn() {
this.x = a;
}
}