Skip to content

Commit a67b80d

Browse files
authored
Merge pull request microsoft#13726 from Microsoft/allow-super-to-access-method-signatures
Allow super to access method signatures
2 parents 9b1dd14 + 4d67b0c commit a67b80d

File tree

5 files changed

+99
-5
lines changed

5 files changed

+99
-5
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12337,12 +12337,15 @@ namespace ts {
1233712337
// - In a static member function or static member accessor
1233812338
// where this references the constructor function object of a derived class,
1233912339
// a super property access is permitted and must specify a public static member function of the base class.
12340-
if (languageVersion < ScriptTarget.ES2015 && getDeclarationKindFromSymbol(prop) !== SyntaxKind.MethodDeclaration) {
12341-
// `prop` refers to a *property* declared in the super class
12342-
// rather than a *method*, so it does not satisfy the above criteria.
12340+
if (languageVersion < ScriptTarget.ES2015) {
12341+
const propKind = getDeclarationKindFromSymbol(prop);
12342+
if (propKind !== SyntaxKind.MethodDeclaration && propKind !== SyntaxKind.MethodSignature) {
12343+
// `prop` refers to a *property* declared in the super class
12344+
// rather than a *method*, so it does not satisfy the above criteria.
1234312345

12344-
error(errorNode, Diagnostics.Only_public_and_protected_methods_of_the_base_class_are_accessible_via_the_super_keyword);
12345-
return false;
12346+
error(errorNode, Diagnostics.Only_public_and_protected_methods_of_the_base_class_are_accessible_via_the_super_keyword);
12347+
return false;
12348+
}
1234612349
}
1234712350

1234812351
if (flags & ModifierFlags.Abstract) {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//// [superHasMethodsFromMergedInterface.ts]
2+
class C { m1() { } }
3+
interface C { m2(): void }
4+
class Sub extends C {
5+
m3() {
6+
super.m2();
7+
}
8+
}
9+
10+
11+
//// [superHasMethodsFromMergedInterface.js]
12+
var __extends = (this && this.__extends) || (function () {
13+
var extendStatics = Object.setPrototypeOf ||
14+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
15+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
16+
return function (d, b) {
17+
extendStatics(d, b);
18+
function __() { this.constructor = d; }
19+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
20+
};
21+
})();
22+
var C = (function () {
23+
function C() {
24+
}
25+
C.prototype.m1 = function () { };
26+
return C;
27+
}());
28+
var Sub = (function (_super) {
29+
__extends(Sub, _super);
30+
function Sub() {
31+
return _super !== null && _super.apply(this, arguments) || this;
32+
}
33+
Sub.prototype.m3 = function () {
34+
_super.prototype.m2.call(this);
35+
};
36+
return Sub;
37+
}(C));
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
=== tests/cases/compiler/superHasMethodsFromMergedInterface.ts ===
2+
class C { m1() { } }
3+
>C : Symbol(C, Decl(superHasMethodsFromMergedInterface.ts, 0, 0), Decl(superHasMethodsFromMergedInterface.ts, 0, 20))
4+
>m1 : Symbol(C.m1, Decl(superHasMethodsFromMergedInterface.ts, 0, 9))
5+
6+
interface C { m2(): void }
7+
>C : Symbol(C, Decl(superHasMethodsFromMergedInterface.ts, 0, 0), Decl(superHasMethodsFromMergedInterface.ts, 0, 20))
8+
>m2 : Symbol(C.m2, Decl(superHasMethodsFromMergedInterface.ts, 1, 13))
9+
10+
class Sub extends C {
11+
>Sub : Symbol(Sub, Decl(superHasMethodsFromMergedInterface.ts, 1, 26))
12+
>C : Symbol(C, Decl(superHasMethodsFromMergedInterface.ts, 0, 0), Decl(superHasMethodsFromMergedInterface.ts, 0, 20))
13+
14+
m3() {
15+
>m3 : Symbol(Sub.m3, Decl(superHasMethodsFromMergedInterface.ts, 2, 21))
16+
17+
super.m2();
18+
>super.m2 : Symbol(C.m2, Decl(superHasMethodsFromMergedInterface.ts, 1, 13))
19+
>super : Symbol(C, Decl(superHasMethodsFromMergedInterface.ts, 0, 0), Decl(superHasMethodsFromMergedInterface.ts, 0, 20))
20+
>m2 : Symbol(C.m2, Decl(superHasMethodsFromMergedInterface.ts, 1, 13))
21+
}
22+
}
23+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=== tests/cases/compiler/superHasMethodsFromMergedInterface.ts ===
2+
class C { m1() { } }
3+
>C : C
4+
>m1 : () => void
5+
6+
interface C { m2(): void }
7+
>C : C
8+
>m2 : () => void
9+
10+
class Sub extends C {
11+
>Sub : Sub
12+
>C : C
13+
14+
m3() {
15+
>m3 : () => void
16+
17+
super.m2();
18+
>super.m2() : void
19+
>super.m2 : () => void
20+
>super : C
21+
>m2 : () => void
22+
}
23+
}
24+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class C { m1() { } }
2+
interface C { m2(): void }
3+
class Sub extends C {
4+
m3() {
5+
super.m2();
6+
}
7+
}

0 commit comments

Comments
 (0)