Skip to content

Commit 30c51ed

Browse files
committed
fix super call from class that has no basetype but with same symbol interface (microsoft#19068)
1 parent d4c0377 commit 30c51ed

6 files changed

Lines changed: 71 additions & 3 deletions

src/compiler/checker.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13043,12 +13043,14 @@ namespace ts {
1304313043

1304413044
// at this point the only legal case for parent is ClassLikeDeclaration
1304513045
const classLikeDeclaration = <ClassLikeDeclaration>container.parent;
13046+
if (!getClassExtendsHeritageClauseElement(classLikeDeclaration)) {
13047+
error(node, Diagnostics.super_can_only_be_referenced_in_a_derived_class);
13048+
return unknownType;
13049+
}
13050+
1304613051
const classType = <InterfaceType>getDeclaredTypeOfSymbol(getSymbolOfNode(classLikeDeclaration));
1304713052
const baseClassType = classType && getBaseTypes(classType)[0];
1304813053
if (!baseClassType) {
13049-
if (!getClassExtendsHeritageClauseElement(classLikeDeclaration)) {
13050-
error(node, Diagnostics.super_can_only_be_referenced_in_a_derived_class);
13051-
}
1305213054
return unknownType;
1305313055
}
1305413056

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
tests/cases/conformance/es6/classDeclaration/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.ts(5,9): error TS2335: 'super' can only be referenced in a derived class.
2+
3+
4+
==== tests/cases/conformance/es6/classDeclaration/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.ts (1 errors) ====
5+
interface Foo extends Array<number> {}
6+
7+
class Foo {
8+
constructor() {
9+
super(); // error
10+
~~~~~
11+
!!! error TS2335: 'super' can only be referenced in a derived class.
12+
}
13+
}
14+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.ts]
2+
interface Foo extends Array<number> {}
3+
4+
class Foo {
5+
constructor() {
6+
super(); // error
7+
}
8+
}
9+
10+
11+
//// [superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.js]
12+
var Foo = /** @class */ (function () {
13+
function Foo() {
14+
_this = _super.call(this) || this; // error
15+
}
16+
return Foo;
17+
}());
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/conformance/es6/classDeclaration/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.ts ===
2+
interface Foo extends Array<number> {}
3+
>Foo : Symbol(Foo, Decl(superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.ts, 0, 0), Decl(superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.ts, 0, 38))
4+
>Array : Symbol(Array, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
5+
6+
class Foo {
7+
>Foo : Symbol(Foo, Decl(superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.ts, 0, 0), Decl(superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.ts, 0, 38))
8+
9+
constructor() {
10+
super(); // error
11+
}
12+
}
13+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/conformance/es6/classDeclaration/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.ts ===
2+
interface Foo extends Array<number> {}
3+
>Foo : Foo
4+
>Array : T[]
5+
6+
class Foo {
7+
>Foo : Foo
8+
9+
constructor() {
10+
super(); // error
11+
>super() : void
12+
>super : any
13+
}
14+
}
15+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
interface Foo extends Array<number> {}
2+
3+
class Foo {
4+
constructor() {
5+
super(); // error
6+
}
7+
}

0 commit comments

Comments
 (0)