Skip to content

Commit dcb763f

Browse files
tanhauhauRyanCavanaugh
authored andcommitted
Report error when using generic class in js file (microsoft#31723)
* Report error when using generic class in js file * Replace "ClassDeclaration | ClassExpression" to ClassLikeDeclaration Co-Authored-By: Klaus Meinhardt <[email protected]> * add noEmit:true
1 parent 334b859 commit dcb763f

5 files changed

+24
-1
lines changed

src/compiler/program.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1833,6 +1833,7 @@ namespace ts {
18331833

18341834
switch (parent.kind) {
18351835
case SyntaxKind.ClassDeclaration:
1836+
case SyntaxKind.ClassExpression:
18361837
case SyntaxKind.MethodDeclaration:
18371838
case SyntaxKind.MethodSignature:
18381839
case SyntaxKind.Constructor:
@@ -1842,7 +1843,7 @@ namespace ts {
18421843
case SyntaxKind.FunctionDeclaration:
18431844
case SyntaxKind.ArrowFunction:
18441845
// Check type parameters
1845-
if (nodes === (<ClassDeclaration | FunctionLikeDeclaration>parent).typeParameters) {
1846+
if (nodes === (<ClassLikeDeclaration | FunctionLikeDeclaration>parent).typeParameters) {
18461847
diagnostics.push(createDiagnosticForNodeArray(nodes, Diagnostics.type_parameter_declarations_can_only_be_used_in_a_ts_file));
18471848
return;
18481849
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
tests/cases/compiler/a.js(1,19): error TS8004: 'type parameter declarations' can only be used in a .ts file.
2+
3+
4+
==== tests/cases/compiler/a.js (1 errors) ====
5+
const Bar = class<T> {};
6+
~
7+
!!! error TS8004: 'type parameter declarations' can only be used in a .ts file.
8+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=== tests/cases/compiler/a.js ===
2+
const Bar = class<T> {};
3+
>Bar : Symbol(Bar, Decl(a.js, 0, 5))
4+
>T : Symbol(T, Decl(a.js, 0, 18))
5+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=== tests/cases/compiler/a.js ===
2+
const Bar = class<T> {};
3+
>Bar : typeof Bar
4+
>class<T> {} : typeof Bar
5+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @allowJs: true
2+
// @noEmit: true
3+
// @filename: a.js
4+
const Bar = class<T> {};

0 commit comments

Comments
 (0)