Skip to content

Commit f83635a

Browse files
committed
When `resolveCall` does not resolve in `resolveNewExpression`, the error should only be thrown if there is a *defined* signature that is not-void.
1 parent a5a26ec commit f83635a

File tree

6 files changed

+51
-9
lines changed

6 files changed

+51
-9
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19592,7 +19592,7 @@ namespace ts {
1959219592
const callSignatures = getSignaturesOfType(expressionType, SignatureKind.Call);
1959319593
if (callSignatures.length) {
1959419594
const signature = resolveCall(node, callSignatures, candidatesOutArray, isForSignatureHelp);
19595-
if (!isJavaScriptConstructor(signature.declaration) && getReturnTypeOfSignature(signature) !== voidType) {
19595+
if (signature.declaration && !isJavaScriptConstructor(signature.declaration) && getReturnTypeOfSignature(signature) !== voidType) {
1959619596
error(node, Diagnostics.Only_a_void_function_can_be_called_with_the_new_keyword);
1959719597
}
1959819598
if (getThisTypeOfSignature(signature) === voidType) {

tests/baselines/reference/constructorFunctions.errors.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
tests/cases/conformance/salsa/index.js(22,15): error TS2348: Value of type 'typeof C3' is not callable. Did you mean to include 'new'?
22
tests/cases/conformance/salsa/index.js(30,15): error TS2348: Value of type 'typeof C4' is not callable. Did you mean to include 'new'?
3+
tests/cases/conformance/salsa/index.js(55,13): error TS2554: Expected 1 arguments, but got 0.
34

45

5-
==== tests/cases/conformance/salsa/index.js (2 errors) ====
6+
==== tests/cases/conformance/salsa/index.js (3 errors) ====
67
function C1() {
78
if (!(this instanceof C1)) return new C1();
89
this.x = 1;
@@ -53,4 +54,15 @@ tests/cases/conformance/salsa/index.js(30,15): error TS2348: Value of type 'type
5354
};
5455

5556
var c6_v1 = new C6();
57+
58+
59+
/**
60+
* @constructor
61+
* @param {number} num
62+
*/
63+
function C7(num) {}
64+
65+
var c7_v1 = new C7();
66+
~~~~~~~~
67+
!!! error TS2554: Expected 1 arguments, but got 0.
5668

tests/baselines/reference/constructorFunctions.symbols

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,16 @@ var c6_v1 = new C6();
116116
>c6_v1 : Symbol(c6_v1, Decl(index.js, 45, 3))
117117
>C6 : Symbol(C6, Decl(index.js, 38, 12))
118118

119+
120+
/**
121+
* @constructor
122+
* @param {number} num
123+
*/
124+
function C7(num) {}
125+
>C7 : Symbol(C7, Decl(index.js, 45, 21))
126+
>num : Symbol(num, Decl(index.js, 52, 12))
127+
128+
var c7_v1 = new C7();
129+
>c7_v1 : Symbol(c7_v1, Decl(index.js, 54, 3))
130+
>C7 : Symbol(C7, Decl(index.js, 45, 21))
131+

tests/baselines/reference/constructorFunctions.types

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,17 @@ var c6_v1 = new C6();
168168
>new C6() : C6
169169
>C6 : typeof C6
170170

171+
172+
/**
173+
* @constructor
174+
* @param {number} num
175+
*/
176+
function C7(num) {}
177+
>C7 : typeof C7
178+
>num : number
179+
180+
var c7_v1 = new C7();
181+
>c7_v1 : any
182+
>new C7() : any
183+
>C7 : typeof C7
184+

tests/baselines/reference/instantiateNonGenericTypeWithTypeArguments.errors.txt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiateNonGenericTypeWithTypeArguments.ts(8,15): error TS2558: Expected 0 type arguments, but got 1.
2-
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiateNonGenericTypeWithTypeArguments.ts(11,9): error TS2350: Only a void function can be called with the 'new' keyword.
32
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiateNonGenericTypeWithTypeArguments.ts(11,17): error TS2558: Expected 0 type arguments, but got 1.
4-
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiateNonGenericTypeWithTypeArguments.ts(14,10): error TS2350: Only a void function can be called with the 'new' keyword.
53
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiateNonGenericTypeWithTypeArguments.ts(14,16): error TS2558: Expected 0 type arguments, but got 1.
64
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiateNonGenericTypeWithTypeArguments.ts(18,10): error TS2347: Untyped function calls may not accept type arguments.
75

86

9-
==== tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiateNonGenericTypeWithTypeArguments.ts (6 errors) ====
7+
==== tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiateNonGenericTypeWithTypeArguments.ts (4 errors) ====
108
// it is an error to provide type arguments to a non-generic call
119
// all of these are errors
1210

@@ -20,15 +18,11 @@ tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiateNonGen
2018

2119
function Foo(): void { }
2220
var r = new Foo<number>();
23-
~~~~~~~~~~~~~~~~~
24-
!!! error TS2350: Only a void function can be called with the 'new' keyword.
2521
~~~~~~
2622
!!! error TS2558: Expected 0 type arguments, but got 1.
2723

2824
var f: { (): void };
2925
var r2 = new f<number>();
30-
~~~~~~~~~~~~~~~
31-
!!! error TS2350: Only a void function can be called with the 'new' keyword.
3226
~~~~~~
3327
!!! error TS2558: Expected 0 type arguments, but got 1.
3428

tests/cases/conformance/salsa/constructorFunctions.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,12 @@ function C6() {
4949
};
5050

5151
var c6_v1 = new C6();
52+
53+
54+
/**
55+
* @constructor
56+
* @param {number} num
57+
*/
58+
function C7(num) {}
59+
60+
var c7_v1 = new C7();

0 commit comments

Comments
 (0)