Skip to content

Commit

Permalink
Merge pull request microsoft#19797 from charlespierce/implements_clas…
Browse files Browse the repository at this point in the history
…s_error_message

Add new error message when class implements class
  • Loading branch information
DanielRosenwasser authored Dec 19, 2017
2 parents 89b5d8c + c489dd9 commit 69e091b
Show file tree
Hide file tree
Showing 14 changed files with 109 additions and 37 deletions.
7 changes: 6 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22740,7 +22740,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);
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* @internal */
namespace ts.codefix {
const errorCodes = [Diagnostics.Class_0_incorrectly_implements_interface_1.code];
const 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];
const fixId = "fixClassIncorrectlyImplementsInterface"; // TODO: share a group with fixClassDoesntImplementInheritedAbstractMember?
registerCodeFix({
errorCodes,
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/classImplementsClass2.errors.txt
Original file line number Diff line number Diff line change
@@ -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'.
Expand All @@ -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() {
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/classImplementsClass4.errors.txt
Original file line number Diff line number Diff line change
@@ -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'.
Expand All @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/classImplementsClass5.errors.txt
Original file line number Diff line number Diff line change
@@ -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'.
Expand All @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions tests/baselines/reference/classImplementsClass7.errors.txt
Original file line number Diff line number Diff line change
@@ -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'.

19 changes: 19 additions & 0 deletions tests/baselines/reference/classImplementsClass7.js
Original file line number Diff line number Diff line change
@@ -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;
}());
12 changes: 12 additions & 0 deletions tests/baselines/reference/classImplementsClass7.symbols
Original file line number Diff line number Diff line change
@@ -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))

12 changes: 12 additions & 0 deletions tests/baselines/reference/classImplementsClass7.types
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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'.


Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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'.
Expand All @@ -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() { }
}

Expand Down
24 changes: 12 additions & 12 deletions tests/baselines/reference/genericSpecializations2.errors.txt
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -21,23 +21,23 @@ 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'.
}

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'.
Expand Down
5 changes: 5 additions & 0 deletions tests/cases/compiler/classImplementsClass7.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class A {
private x: number;
}

class B implements A {}

0 comments on commit 69e091b

Please sign in to comment.