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
This commit is contained in:
Herrington Darkholme 2017-09-12 02:21:35 +08:00 committed by Mohamed Hegazy
parent eb80799ef0
commit 29d5e4dadd
4 changed files with 36 additions and 10 deletions

View file

@ -16128,16 +16128,6 @@ namespace ts {
return resolveErrorCall(node);
}
// If the expression is a class of abstract type, then it cannot be instantiated.
// Note, only class declarations can be declared abstract.
// In the case of a merged class-module or class-interface declaration,
// only the class declaration node will have the Abstract flag set.
const valueDecl = expressionType.symbol && getClassLikeDeclarationOfSymbol(expressionType.symbol);
if (valueDecl && hasModifier(valueDecl, ModifierFlags.Abstract)) {
error(node, Diagnostics.Cannot_create_an_instance_of_the_abstract_class_0, declarationNameToString(getNameOfDeclaration(valueDecl)));
return resolveErrorCall(node);
}
// TS 1.0 spec: 4.11
// If expressionType is of type Any, Args can be any argument
// list and the result of the operation is of type Any.
@ -16157,6 +16147,16 @@ namespace ts {
if (!isConstructorAccessible(node, constructSignatures[0])) {
return resolveErrorCall(node);
}
// If the expression is a class of abstract type, then it cannot be instantiated.
// Note, only class declarations can be declared abstract.
// In the case of a merged class-module or class-interface declaration,
// only the class declaration node will have the Abstract flag set.
const valueDecl = expressionType.symbol && getClassLikeDeclarationOfSymbol(expressionType.symbol);
if (valueDecl && hasModifier(valueDecl, ModifierFlags.Abstract)) {
error(node, Diagnostics.Cannot_create_an_instance_of_the_abstract_class_0, declarationNameToString(getNameOfDeclaration(valueDecl)));
return resolveErrorCall(node);
}
return resolveCall(node, constructSignatures, candidatesOutArray);
}

View file

@ -0,0 +1,10 @@
tests/cases/compiler/newAbstractInstance.ts(3,1): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.
==== tests/cases/compiler/newAbstractInstance.ts (1 errors) ====
abstract class B { }
declare const b: B;
new b();
~~~~~~~
!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.

View file

@ -0,0 +1,13 @@
//// [newAbstractInstance.ts]
abstract class B { }
declare const b: B;
new b();
//// [newAbstractInstance.js]
var B = /** @class */ (function () {
function B() {
}
return B;
}());
new b();

View file

@ -0,0 +1,3 @@
abstract class B { }
declare const b: B;
new b();