mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-13 14:09:06 -05:00
* 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:
committed by
GitHub
parent
9b9ec6309e
commit
13734e7d68
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
@@ -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'.
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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) {
|
||||
},
|
||||
|
||||
@@ -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))
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
}
|
||||
37
tests/baselines/reference/propertyOverridingPrototype.js
Normal file
37
tests/baselines/reference/propertyOverridingPrototype.js
Normal 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));
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
|
||||
18
tests/baselines/reference/propertyOverridingPrototype.types
Normal file
18
tests/baselines/reference/propertyOverridingPrototype.types
Normal 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 : () => {}
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
}
|
||||
}
|
||||
9
tests/cases/compiler/propertyOverridingPrototype.ts
Normal file
9
tests/cases/compiler/propertyOverridingPrototype.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
class Base {
|
||||
foo() {
|
||||
}
|
||||
}
|
||||
|
||||
class Derived extends Base {
|
||||
foo: () => { };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user