Skip to content

Commit 2b10784

Browse files
authored
Merge pull request #17922 from Microsoft/baseExpressionTypeParameters
Class type parameters not in scope in base class expression
2 parents ac09853 + 914d428 commit 2b10784

8 files changed

+106
-9
lines changed

src/compiler/checker.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,18 @@ namespace ts {
10161016
}
10171017
}
10181018
break;
1019-
1019+
case SyntaxKind.ExpressionWithTypeArguments:
1020+
// The type parameters of a class are not in scope in the base class expression.
1021+
if (lastLocation === (<ExpressionWithTypeArguments>location).expression && (<HeritageClause>location.parent).token === SyntaxKind.ExtendsKeyword) {
1022+
const container = location.parent.parent;
1023+
if (isClassLike(container) && (result = lookup(getSymbolOfNode(container).members, name, meaning & SymbolFlags.Type))) {
1024+
if (nameNotFoundMessage) {
1025+
error(errorLocation, Diagnostics.Base_class_expressions_cannot_reference_class_type_parameters);
1026+
}
1027+
return undefined;
1028+
}
1029+
}
1030+
break;
10201031
// It is not legal to reference a class's own type parameters from a computed property name that
10211032
// belongs to the class. For example:
10221033
//

src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -1912,6 +1912,10 @@
19121912
"category": "Error",
19131913
"code": 2560
19141914
},
1915+
"Base class expressions cannot reference class type parameters.": {
1916+
"category": "Error",
1917+
"code": 2561
1918+
},
19151919
"JSX element attributes type '{0}' may not be a union type.": {
19161920
"category": "Error",
19171921
"code": 2600
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
tests/cases/compiler/baseExpressionTypeParameters.ts(10,27): error TS2561: Base class expressions cannot reference class type parameters.
2+
3+
4+
==== tests/cases/compiler/baseExpressionTypeParameters.ts (1 errors) ====
5+
// Repro from #17829
6+
7+
function base<T>() {
8+
class Base {
9+
static prop: T;
10+
}
11+
return Base;
12+
}
13+
14+
class Gen<T> extends base<T>() {} // Error, T not in scope
15+
~
16+
!!! error TS2561: Base class expressions cannot reference class type parameters.
17+
class Spec extends Gen<string> {}
18+
19+
<string>Spec.prop;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//// [baseExpressionTypeParameters.ts]
2+
// Repro from #17829
3+
4+
function base<T>() {
5+
class Base {
6+
static prop: T;
7+
}
8+
return Base;
9+
}
10+
11+
class Gen<T> extends base<T>() {} // Error, T not in scope
12+
class Spec extends Gen<string> {}
13+
14+
<string>Spec.prop;
15+
16+
//// [baseExpressionTypeParameters.js]
17+
// Repro from #17829
18+
var __extends = (this && this.__extends) || (function () {
19+
var extendStatics = Object.setPrototypeOf ||
20+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
21+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
22+
return function (d, b) {
23+
extendStatics(d, b);
24+
function __() { this.constructor = d; }
25+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
26+
};
27+
})();
28+
function base() {
29+
var Base = /** @class */ (function () {
30+
function Base() {
31+
}
32+
return Base;
33+
}());
34+
return Base;
35+
}
36+
var Gen = /** @class */ (function (_super) {
37+
__extends(Gen, _super);
38+
function Gen() {
39+
return _super !== null && _super.apply(this, arguments) || this;
40+
}
41+
return Gen;
42+
}(base())); // Error, T not in scope
43+
var Spec = /** @class */ (function (_super) {
44+
__extends(Spec, _super);
45+
function Spec() {
46+
return _super !== null && _super.apply(this, arguments) || this;
47+
}
48+
return Spec;
49+
}(Gen));
50+
Spec.prop;
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
tests/cases/compiler/inheritFromGenericTypeParameter.ts(1,20): error TS2693: 'T' only refers to a type, but is being used as a value here.
1+
tests/cases/compiler/inheritFromGenericTypeParameter.ts(1,20): error TS2304: Cannot find name 'T'.
22
tests/cases/compiler/inheritFromGenericTypeParameter.ts(2,24): error TS2312: An interface may only extend a class or another interface.
33

44

55
==== tests/cases/compiler/inheritFromGenericTypeParameter.ts (2 errors) ====
66
class C<T> extends T { }
77
~
8-
!!! error TS2693: 'T' only refers to a type, but is being used as a value here.
8+
!!! error TS2304: Cannot find name 'T'.
99
interface I<T> extends T { }
1010
~
1111
!!! error TS2312: An interface may only extend a class or another interface.
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
tests/cases/compiler/typeParameterAsBaseClass.ts(1,20): error TS2693: 'T' only refers to a type, but is being used as a value here.
1+
tests/cases/compiler/typeParameterAsBaseClass.ts(1,20): error TS2304: Cannot find name 'T'.
22
tests/cases/compiler/typeParameterAsBaseClass.ts(2,24): error TS2422: A class may only implement another class or interface.
33

44

55
==== tests/cases/compiler/typeParameterAsBaseClass.ts (2 errors) ====
66
class C<T> extends T {}
77
~
8-
!!! error TS2693: 'T' only refers to a type, but is being used as a value here.
8+
!!! error TS2304: Cannot find name 'T'.
99
class C2<T> implements T {}
1010
~
1111
!!! error TS2422: A class may only implement another class or interface.

tests/baselines/reference/typeParameterAsBaseType.errors.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
tests/cases/conformance/types/typeParameters/typeParameterAsBaseType.ts(4,20): error TS2693: 'T' only refers to a type, but is being used as a value here.
2-
tests/cases/conformance/types/typeParameters/typeParameterAsBaseType.ts(5,24): error TS2693: 'U' only refers to a type, but is being used as a value here.
1+
tests/cases/conformance/types/typeParameters/typeParameterAsBaseType.ts(4,20): error TS2304: Cannot find name 'T'.
2+
tests/cases/conformance/types/typeParameters/typeParameterAsBaseType.ts(5,24): error TS2304: Cannot find name 'U'.
33
tests/cases/conformance/types/typeParameters/typeParameterAsBaseType.ts(7,24): error TS2312: An interface may only extend a class or another interface.
44
tests/cases/conformance/types/typeParameters/typeParameterAsBaseType.ts(8,28): error TS2312: An interface may only extend a class or another interface.
55

@@ -10,10 +10,10 @@ tests/cases/conformance/types/typeParameters/typeParameterAsBaseType.ts(8,28): e
1010

1111
class C<T> extends T { }
1212
~
13-
!!! error TS2693: 'T' only refers to a type, but is being used as a value here.
13+
!!! error TS2304: Cannot find name 'T'.
1414
class C2<T, U> extends U { }
1515
~
16-
!!! error TS2693: 'U' only refers to a type, but is being used as a value here.
16+
!!! error TS2304: Cannot find name 'U'.
1717

1818
interface I<T> extends T { }
1919
~
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Repro from #17829
2+
3+
function base<T>() {
4+
class Base {
5+
static prop: T;
6+
}
7+
return Base;
8+
}
9+
10+
class Gen<T> extends base<T>() {} // Error, T not in scope
11+
class Spec extends Gen<string> {}
12+
13+
<string>Spec.prop;

0 commit comments

Comments
 (0)