Add new error message when class implements class #19793

This commit is contained in:
Charles Pierce 2017-12-05 21:18:31 -08:00
parent 3a3bb8e3f0
commit 472f087e69
14 changed files with 109 additions and 37 deletions

View file

@ -22657,7 +22657,12 @@ namespace ts {
const t = getTypeFromTypeNode(typeRefNode);
if (t !== unknownType) {
if (isValidBaseType(t)) {
checkTypeAssignableTo(typeWithThis, getTypeWithThisArgument(t, type.thisType), node.name || node, Diagnostics.Class_0_incorrectly_implements_interface_1);
checkTypeAssignableTo(typeWithThis,
getTypeWithThisArgument(t, type.thisType),
node.name || node,
t.symbol.flags & SymbolFlags.Class ?
Diagnostics.Class_0_incorrectly_implements_class_1_Did_you_mean_to_extend_1_and_inherit_its_members_as_a_subclass :
Diagnostics.Class_0_incorrectly_implements_interface_1);
}
else {
error(typeRefNode, Diagnostics.A_class_may_only_implement_another_class_or_interface);

View file

@ -2276,6 +2276,10 @@
"category": "Error",
"code": 2719
},
"Class '{0}' incorrectly implements class '{1}'. Did you mean to extend '{1}' and inherit its members as a subclass?": {
"category": "Error",
"code": 2720
},
"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",

View file

@ -1,7 +1,8 @@
/* @internal */
namespace ts.codefix {
registerCodeFix({
errorCodes: [Diagnostics.Class_0_incorrectly_implements_interface_1.code],
errorCodes: [Diagnostics.Class_0_incorrectly_implements_interface_1.code,
Diagnostics.Class_0_incorrectly_implements_class_1_Did_you_mean_to_extend_1_and_inherit_its_members_as_a_subclass.code],
getCodeActions: getActionForClassLikeIncorrectImplementsInterface
});

View file

@ -1,4 +1,4 @@
tests/cases/compiler/classImplementsClass2.ts(2,7): error TS2420: Class 'C' incorrectly implements interface 'A'.
tests/cases/compiler/classImplementsClass2.ts(2,7): error TS2720: Class 'C' incorrectly implements class 'A'. Did you mean to extend 'A' and inherit its members as a subclass?
Property 'foo' is missing in type 'C'.
tests/cases/compiler/classImplementsClass2.ts(13,1): error TS2322: Type 'C' is not assignable to type 'C2'.
Property 'foo' is missing in type 'C'.
@ -8,8 +8,8 @@ tests/cases/compiler/classImplementsClass2.ts(13,1): error TS2322: Type 'C' is n
class A { foo(): number { return 1; } }
class C implements A {} // error
~
!!! error TS2420: Class 'C' incorrectly implements interface 'A'.
!!! error TS2420: Property 'foo' is missing in type 'C'.
!!! error TS2720: Class 'C' incorrectly implements class 'A'. Did you mean to extend 'A' and inherit its members as a subclass?
!!! error TS2720: Property 'foo' is missing in type 'C'.
class C2 extends A {
foo() {

View file

@ -1,4 +1,4 @@
tests/cases/compiler/classImplementsClass4.ts(5,7): error TS2420: Class 'C' incorrectly implements interface 'A'.
tests/cases/compiler/classImplementsClass4.ts(5,7): error TS2720: Class 'C' incorrectly implements class 'A'. Did you mean to extend 'A' and inherit its members as a subclass?
Property 'x' is missing in type 'C'.
tests/cases/compiler/classImplementsClass4.ts(16,1): error TS2322: Type 'C' is not assignable to type 'C2'.
Property 'x' is missing in type 'C'.
@ -11,8 +11,8 @@ tests/cases/compiler/classImplementsClass4.ts(16,1): error TS2322: Type 'C' is n
}
class C implements A {
~
!!! error TS2420: Class 'C' incorrectly implements interface 'A'.
!!! error TS2420: Property 'x' is missing in type 'C'.
!!! error TS2720: Class 'C' incorrectly implements class 'A'. Did you mean to extend 'A' and inherit its members as a subclass?
!!! error TS2720: Property 'x' is missing in type 'C'.
foo() {
return 1;
}

View file

@ -1,4 +1,4 @@
tests/cases/compiler/classImplementsClass5.ts(5,7): error TS2420: Class 'C' incorrectly implements interface 'A'.
tests/cases/compiler/classImplementsClass5.ts(5,7): error TS2720: Class 'C' incorrectly implements class 'A'. Did you mean to extend 'A' and inherit its members as a subclass?
Types have separate declarations of a private property 'x'.
tests/cases/compiler/classImplementsClass5.ts(16,1): error TS2322: Type 'C2' is not assignable to type 'C'.
Types have separate declarations of a private property 'x'.
@ -13,8 +13,8 @@ tests/cases/compiler/classImplementsClass5.ts(17,1): error TS2322: Type 'C' is n
}
class C implements A {
~
!!! error TS2420: Class 'C' incorrectly implements interface 'A'.
!!! error TS2420: Types have separate declarations of a private property 'x'.
!!! error TS2720: Class 'C' incorrectly implements class 'A'. Did you mean to extend 'A' and inherit its members as a subclass?
!!! error TS2720: Types have separate declarations of a private property 'x'.
private x = 1;
foo() {
return 1;

View file

@ -0,0 +1,14 @@
tests/cases/compiler/classImplementsClass7.ts(5,7): error TS2720: Class 'B' incorrectly implements class 'A'. Did you mean to extend 'A' and inherit its members as a subclass?
Property 'x' is missing in type 'B'.
==== tests/cases/compiler/classImplementsClass7.ts (1 errors) ====
class A {
private x: number;
}
class B implements A {}
~
!!! error TS2720: Class 'B' incorrectly implements class 'A'. Did you mean to extend 'A' and inherit its members as a subclass?
!!! error TS2720: Property 'x' is missing in type 'B'.

View file

@ -0,0 +1,19 @@
//// [classImplementsClass7.ts]
class A {
private x: number;
}
class B implements A {}
//// [classImplementsClass7.js]
var A = /** @class */ (function () {
function A() {
}
return A;
}());
var B = /** @class */ (function () {
function B() {
}
return B;
}());

View file

@ -0,0 +1,12 @@
=== tests/cases/compiler/classImplementsClass7.ts ===
class A {
>A : Symbol(A, Decl(classImplementsClass7.ts, 0, 0))
private x: number;
>x : Symbol(A.x, Decl(classImplementsClass7.ts, 0, 9))
}
class B implements A {}
>B : Symbol(B, Decl(classImplementsClass7.ts, 2, 1))
>A : Symbol(A, Decl(classImplementsClass7.ts, 0, 0))

View file

@ -0,0 +1,12 @@
=== tests/cases/compiler/classImplementsClass7.ts ===
class A {
>A : A
private x: number;
>x : number
}
class B implements A {}
>B : B
>A : A

View file

@ -1,8 +1,8 @@
tests/cases/conformance/classes/classDeclarations/classImplementsMergedClassInterface.ts(9,7): error TS2420: Class 'C2' incorrectly implements interface 'C1'.
tests/cases/conformance/classes/classDeclarations/classImplementsMergedClassInterface.ts(9,7): error TS2720: Class 'C2' incorrectly implements class 'C1'. Did you mean to extend 'C1' and inherit its members as a subclass?
Property 'x' is missing in type 'C2'.
tests/cases/conformance/classes/classDeclarations/classImplementsMergedClassInterface.ts(12,7): error TS2420: Class 'C3' incorrectly implements interface 'C1'.
tests/cases/conformance/classes/classDeclarations/classImplementsMergedClassInterface.ts(12,7): error TS2720: Class 'C3' incorrectly implements class 'C1'. Did you mean to extend 'C1' and inherit its members as a subclass?
Property 'y' is missing in type 'C3'.
tests/cases/conformance/classes/classDeclarations/classImplementsMergedClassInterface.ts(16,7): error TS2420: Class 'C4' incorrectly implements interface 'C1'.
tests/cases/conformance/classes/classDeclarations/classImplementsMergedClassInterface.ts(16,7): error TS2720: Class 'C4' incorrectly implements class 'C1'. Did you mean to extend 'C1' and inherit its members as a subclass?
Property 'x' is missing in type 'C4'.
@ -17,21 +17,21 @@ tests/cases/conformance/classes/classDeclarations/classImplementsMergedClassInte
class C2 implements C1 { // error -- missing x
~~
!!! error TS2420: Class 'C2' incorrectly implements interface 'C1'.
!!! error TS2420: Property 'x' is missing in type 'C2'.
!!! error TS2720: Class 'C2' incorrectly implements class 'C1'. Did you mean to extend 'C1' and inherit its members as a subclass?
!!! error TS2720: Property 'x' is missing in type 'C2'.
}
class C3 implements C1 { // error -- missing y
~~
!!! error TS2420: Class 'C3' incorrectly implements interface 'C1'.
!!! error TS2420: Property 'y' is missing in type 'C3'.
!!! error TS2720: Class 'C3' incorrectly implements class 'C1'. Did you mean to extend 'C1' and inherit its members as a subclass?
!!! error TS2720: Property 'y' is missing in type 'C3'.
x : number;
}
class C4 implements C1 { // error -- missing x
~~
!!! error TS2420: Class 'C4' incorrectly implements interface 'C1'.
!!! error TS2420: Property 'x' is missing in type 'C4'.
!!! error TS2720: Class 'C4' incorrectly implements class 'C1'. Did you mean to extend 'C1' and inherit its members as a subclass?
!!! error TS2720: Property 'x' is missing in type 'C4'.
y : number;
}

View file

@ -1,4 +1,4 @@
tests/cases/compiler/extendAndImplementTheSameBaseType2.ts(7,7): error TS2420: Class 'D' incorrectly implements interface 'C<number>'.
tests/cases/compiler/extendAndImplementTheSameBaseType2.ts(7,7): error TS2720: Class 'D' incorrectly implements class 'C<number>'. Did you mean to extend 'C<number>' and inherit its members as a subclass?
Types of property 'bar' are incompatible.
Type '() => string' is not assignable to type '() => number'.
Type 'string' is not assignable to type 'number'.
@ -15,10 +15,10 @@ tests/cases/compiler/extendAndImplementTheSameBaseType2.ts(16,5): error TS2322:
}
class D extends C<string> implements C<number> {
~
!!! error TS2420: Class 'D' incorrectly implements interface 'C<number>'.
!!! error TS2420: Types of property 'bar' are incompatible.
!!! error TS2420: Type '() => string' is not assignable to type '() => number'.
!!! error TS2420: Type 'string' is not assignable to type 'number'.
!!! error TS2720: Class 'D' incorrectly implements class 'C<number>'. Did you mean to extend 'C<number>' and inherit its members as a subclass?
!!! error TS2720: Types of property 'bar' are incompatible.
!!! error TS2720: Type '() => string' is not assignable to type '() => number'.
!!! error TS2720: Type 'string' is not assignable to type 'number'.
baz() { }
}

View file

@ -1,10 +1,10 @@
tests/cases/compiler/genericSpecializations2.ts(7,7): error TS2420: Class 'IntFooBad' incorrectly implements interface 'IFoo<number>'.
tests/cases/compiler/genericSpecializations2.ts(7,7): error TS2720: Class 'IntFooBad' incorrectly implements class 'IFoo<number>'. Did you mean to extend 'IFoo<number>' and inherit its members as a subclass?
Types of property 'foo' are incompatible.
Type '<string>(x: string) => string' is not assignable to type '<T>(x: T) => T'.
Types of parameters 'x' and 'x' are incompatible.
Type 'T' is not assignable to type 'string'.
tests/cases/compiler/genericSpecializations2.ts(8,9): error TS2368: Type parameter name cannot be 'string'.
tests/cases/compiler/genericSpecializations2.ts(11,7): error TS2420: Class 'StringFoo2' incorrectly implements interface 'IFoo<string>'.
tests/cases/compiler/genericSpecializations2.ts(11,7): error TS2720: Class 'StringFoo2' incorrectly implements class 'IFoo<string>'. Did you mean to extend 'IFoo<string>' and inherit its members as a subclass?
Types of property 'foo' are incompatible.
Type '<string>(x: string) => string' is not assignable to type '<T>(x: T) => T'.
Types of parameters 'x' and 'x' are incompatible.
@ -21,11 +21,11 @@ tests/cases/compiler/genericSpecializations2.ts(12,9): error TS2368: Type parame
class IntFooBad implements IFoo<number> {
~~~~~~~~~
!!! error TS2420: Class 'IntFooBad' incorrectly implements interface 'IFoo<number>'.
!!! error TS2420: Types of property 'foo' are incompatible.
!!! error TS2420: Type '<string>(x: string) => string' is not assignable to type '<T>(x: T) => T'.
!!! error TS2420: Types of parameters 'x' and 'x' are incompatible.
!!! error TS2420: Type 'T' is not assignable to type 'string'.
!!! error TS2720: Class 'IntFooBad' incorrectly implements class 'IFoo<number>'. Did you mean to extend 'IFoo<number>' and inherit its members as a subclass?
!!! error TS2720: Types of property 'foo' are incompatible.
!!! error TS2720: Type '<string>(x: string) => string' is not assignable to type '<T>(x: T) => T'.
!!! error TS2720: Types of parameters 'x' and 'x' are incompatible.
!!! error TS2720: Type 'T' is not assignable to type 'string'.
foo<string>(x: string): string { return null; }
~~~~~~
!!! error TS2368: Type parameter name cannot be 'string'.
@ -33,11 +33,11 @@ tests/cases/compiler/genericSpecializations2.ts(12,9): error TS2368: Type parame
class StringFoo2 implements IFoo<string> {
~~~~~~~~~~
!!! error TS2420: Class 'StringFoo2' incorrectly implements interface 'IFoo<string>'.
!!! error TS2420: Types of property 'foo' are incompatible.
!!! error TS2420: Type '<string>(x: string) => string' is not assignable to type '<T>(x: T) => T'.
!!! error TS2420: Types of parameters 'x' and 'x' are incompatible.
!!! error TS2420: Type 'T' is not assignable to type 'string'.
!!! error TS2720: Class 'StringFoo2' incorrectly implements class 'IFoo<string>'. Did you mean to extend 'IFoo<string>' and inherit its members as a subclass?
!!! error TS2720: Types of property 'foo' are incompatible.
!!! error TS2720: Type '<string>(x: string) => string' is not assignable to type '<T>(x: T) => T'.
!!! error TS2720: Types of parameters 'x' and 'x' are incompatible.
!!! error TS2720: Type 'T' is not assignable to type 'string'.
foo<string>(x: string): string { return null; }
~~~~~~
!!! error TS2368: Type parameter name cannot be 'string'.

View file

@ -0,0 +1,5 @@
class A {
private x: number;
}
class B implements A {}