Skip to content

Commit 29d5e4d

Browse files
HerringtonDarkholmemhegazy
authored andcommitted
fix #18225, fix error message on abstract class instance (#18368)
* fix #18225, fix error message on abstract class instance abstract class check should be inside constructor call * add new test and accept baseline
1 parent eb80799 commit 29d5e4d

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

src/compiler/checker.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16128,16 +16128,6 @@ namespace ts {
1612816128
return resolveErrorCall(node);
1612916129
}
1613016130

16131-
// If the expression is a class of abstract type, then it cannot be instantiated.
16132-
// Note, only class declarations can be declared abstract.
16133-
// In the case of a merged class-module or class-interface declaration,
16134-
// only the class declaration node will have the Abstract flag set.
16135-
const valueDecl = expressionType.symbol && getClassLikeDeclarationOfSymbol(expressionType.symbol);
16136-
if (valueDecl && hasModifier(valueDecl, ModifierFlags.Abstract)) {
16137-
error(node, Diagnostics.Cannot_create_an_instance_of_the_abstract_class_0, declarationNameToString(getNameOfDeclaration(valueDecl)));
16138-
return resolveErrorCall(node);
16139-
}
16140-
1614116131
// TS 1.0 spec: 4.11
1614216132
// If expressionType is of type Any, Args can be any argument
1614316133
// list and the result of the operation is of type Any.
@@ -16157,6 +16147,16 @@ namespace ts {
1615716147
if (!isConstructorAccessible(node, constructSignatures[0])) {
1615816148
return resolveErrorCall(node);
1615916149
}
16150+
// If the expression is a class of abstract type, then it cannot be instantiated.
16151+
// Note, only class declarations can be declared abstract.
16152+
// In the case of a merged class-module or class-interface declaration,
16153+
// only the class declaration node will have the Abstract flag set.
16154+
const valueDecl = expressionType.symbol && getClassLikeDeclarationOfSymbol(expressionType.symbol);
16155+
if (valueDecl && hasModifier(valueDecl, ModifierFlags.Abstract)) {
16156+
error(node, Diagnostics.Cannot_create_an_instance_of_the_abstract_class_0, declarationNameToString(getNameOfDeclaration(valueDecl)));
16157+
return resolveErrorCall(node);
16158+
}
16159+
1616016160
return resolveCall(node, constructSignatures, candidatesOutArray);
1616116161
}
1616216162

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
tests/cases/compiler/newAbstractInstance.ts(3,1): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.
2+
3+
4+
==== tests/cases/compiler/newAbstractInstance.ts (1 errors) ====
5+
abstract class B { }
6+
declare const b: B;
7+
new b();
8+
~~~~~~~
9+
!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.
10+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [newAbstractInstance.ts]
2+
abstract class B { }
3+
declare const b: B;
4+
new b();
5+
6+
7+
//// [newAbstractInstance.js]
8+
var B = /** @class */ (function () {
9+
function B() {
10+
}
11+
return B;
12+
}());
13+
new b();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
abstract class B { }
2+
declare const b: B;
3+
new b();

0 commit comments

Comments
 (0)