Fix for issue #6154 - overriding methods with properties in the derived class (#24343)

* Fix to issue 6154 - Overriding a method with a property in the derived class should not cause a compiler error

* new baselines

* fixed deleted baselines
This commit is contained in:
Elizabeth Dinella
2018-05-24 14:12:13 -07:00
committed by GitHub
parent 9b9ec6309e
commit 13734e7d68
12 changed files with 101 additions and 93 deletions

View File

@@ -24489,7 +24489,7 @@ namespace ts {
continue;
}
if (isPrototypeProperty(base) && isPrototypeProperty(derived) || base.flags & SymbolFlags.PropertyOrAccessor && derived.flags & SymbolFlags.PropertyOrAccessor) {
if (isPrototypeProperty(base) || base.flags & SymbolFlags.PropertyOrAccessor && derived.flags & SymbolFlags.PropertyOrAccessor) {
// method is overridden with method or property/accessor is overridden with property/accessor - correct case
continue;
}

View File

@@ -1,25 +0,0 @@
tests/cases/compiler/a.js(14,10): error TS2424: Class 'A' defines instance member function 'foo', but extended class 'B' defines it as instance member property.
==== tests/cases/compiler/a.js (1 errors) ====
// @ts-check
class A {
constructor() {
}
foo() {
return 4;
}
}
class B extends A {
constructor() {
super();
this.foo = () => 3;
~~~
!!! error TS2424: Class 'A' defines instance member function 'foo', but extended class 'B' defines it as instance member property.
}
}
const i = new B();
i.foo();

View File

@@ -1,36 +0,0 @@
tests/cases/compiler/inheritanceMemberAccessorOverridingMethod.ts(8,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
tests/cases/compiler/inheritanceMemberAccessorOverridingMethod.ts(8,9): error TS2416: Property 'x' in type 'b' is not assignable to the same property in base type 'a'.
Type 'string' is not assignable to type '() => string'.
tests/cases/compiler/inheritanceMemberAccessorOverridingMethod.ts(8,9): error TS2423: Class 'a' defines instance member function 'x', but extended class 'b' defines it as instance member accessor.
tests/cases/compiler/inheritanceMemberAccessorOverridingMethod.ts(11,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
tests/cases/compiler/inheritanceMemberAccessorOverridingMethod.ts(11,9): error TS2416: Property 'x' in type 'b' is not assignable to the same property in base type 'a'.
Type 'string' is not assignable to type '() => string'.
==== tests/cases/compiler/inheritanceMemberAccessorOverridingMethod.ts (5 errors) ====
class a {
x() {
return "20";
}
}
class b extends a {
get x() {
~
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
~
!!! error TS2416: Property 'x' in type 'b' is not assignable to the same property in base type 'a'.
!!! error TS2416: Type 'string' is not assignable to type '() => string'.
~
!!! error TS2423: Class 'a' defines instance member function 'x', but extended class 'b' defines it as instance member accessor.
return "20";
}
set x(aValue: string) {
~
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
~
!!! error TS2416: Property 'x' in type 'b' is not assignable to the same property in base type 'a'.
!!! error TS2416: Type 'string' is not assignable to type '() => string'.
}
}

View File

@@ -7,10 +7,10 @@ class a {
class b extends a {
get x() {
return "20";
return () => "20";
}
set x(aValue: string) {
set x(aValue) {
}
}
@@ -40,7 +40,7 @@ var b = /** @class */ (function (_super) {
}
Object.defineProperty(b.prototype, "x", {
get: function () {
return "20";
return function () { return "20"; };
},
set: function (aValue) {
},

View File

@@ -16,11 +16,11 @@ class b extends a {
get x() {
>x : Symbol(b.x, Decl(inheritanceMemberAccessorOverridingMethod.ts, 6, 19), Decl(inheritanceMemberAccessorOverridingMethod.ts, 9, 5))
return "20";
return () => "20";
}
set x(aValue: string) {
set x(aValue) {
>x : Symbol(b.x, Decl(inheritanceMemberAccessorOverridingMethod.ts, 6, 19), Decl(inheritanceMemberAccessorOverridingMethod.ts, 9, 5))
>aValue : Symbol(aValue, Decl(inheritanceMemberAccessorOverridingMethod.ts, 10, 10))
}
}

View File

@@ -15,14 +15,15 @@ class b extends a {
>a : a
get x() {
>x : string
>x : () => string
return "20";
return () => "20";
>() => "20" : () => string
>"20" : "20"
}
set x(aValue: string) {
>x : string
>aValue : string
set x(aValue) {
>x : () => string
>aValue : () => string
}
}

View File

@@ -1,15 +0,0 @@
tests/cases/compiler/inheritanceMemberPropertyOverridingMethod.ts(8,5): error TS2424: Class 'a' defines instance member function 'x', but extended class 'b' defines it as instance member property.
==== tests/cases/compiler/inheritanceMemberPropertyOverridingMethod.ts (1 errors) ====
class a {
x() {
return "20";
}
}
class b extends a {
x: () => string;
~
!!! error TS2424: Class 'a' defines instance member function 'x', but extended class 'b' defines it as instance member property.
}

View File

@@ -0,0 +1,37 @@
//// [propertyOverridingPrototype.ts]
class Base {
foo() {
}
}
class Derived extends Base {
foo: () => { };
}
//// [propertyOverridingPrototype.js]
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var Base = /** @class */ (function () {
function Base() {
}
Base.prototype.foo = function () {
};
return Base;
}());
var Derived = /** @class */ (function (_super) {
__extends(Derived, _super);
function Derived() {
return _super !== null && _super.apply(this, arguments) || this;
}
return Derived;
}(Base));

View File

@@ -0,0 +1,18 @@
=== tests/cases/compiler/propertyOverridingPrototype.ts ===
class Base {
>Base : Symbol(Base, Decl(propertyOverridingPrototype.ts, 0, 0))
foo() {
>foo : Symbol(Base.foo, Decl(propertyOverridingPrototype.ts, 0, 12))
}
}
class Derived extends Base {
>Derived : Symbol(Derived, Decl(propertyOverridingPrototype.ts, 3, 1))
>Base : Symbol(Base, Decl(propertyOverridingPrototype.ts, 0, 0))
foo: () => { };
>foo : Symbol(Derived.foo, Decl(propertyOverridingPrototype.ts, 5, 28))
}

View File

@@ -0,0 +1,18 @@
=== tests/cases/compiler/propertyOverridingPrototype.ts ===
class Base {
>Base : Base
foo() {
>foo : () => void
}
}
class Derived extends Base {
>Derived : Derived
>Base : Base
foo: () => { };
>foo : () => {}
}

View File

@@ -1,3 +1,4 @@
// @target: es5
class a {
x() {
return "20";
@@ -6,9 +7,9 @@ class a {
class b extends a {
get x() {
return "20";
return () => "20";
}
set x(aValue: string) {
set x(aValue) {
}
}

View File

@@ -0,0 +1,9 @@
class Base {
foo() {
}
}
class Derived extends Base {
foo: () => { };
}