Skip to content

Commit 50f442f

Browse files
jameskeanesandersn
authored andcommitted
Fixes #26122 - erroneous "TS2350" for js constructors called with incorrect parameters (#26124)
* Fixes #26122. When `resolveCall` does not resolve in `resolveNewExpression`, the error should only be thrown if there is a *defined* signature that is not-void. * Fix other baselines to remove erroneous TS2350.
1 parent a5a26ec commit 50f442f

10 files changed

+55
-25
lines changed

src/compiler/checker.ts

+1-1
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/callOnInstance.errors.txt

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
tests/cases/compiler/callOnInstance.ts(1,18): error TS2300: Duplicate identifier 'D'.
22
tests/cases/compiler/callOnInstance.ts(3,15): error TS2300: Duplicate identifier 'D'.
3-
tests/cases/compiler/callOnInstance.ts(7,19): error TS2350: Only a void function can be called with the 'new' keyword.
43
tests/cases/compiler/callOnInstance.ts(7,19): error TS2554: Expected 0 arguments, but got 1.
54
tests/cases/compiler/callOnInstance.ts(10,1): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'C' has no compatible call signatures.
65

76

8-
==== tests/cases/compiler/callOnInstance.ts (5 errors) ====
7+
==== tests/cases/compiler/callOnInstance.ts (4 errors) ====
98
declare function D(): string; // error
109
~
1110
!!! error TS2300: Duplicate identifier 'D'.
@@ -18,8 +17,6 @@ tests/cases/compiler/callOnInstance.ts(10,1): error TS2349: Cannot invoke an exp
1817

1918
var s2: string = (new D(1))();
2019
~~~~~~~~
21-
!!! error TS2350: Only a void function can be called with the 'new' keyword.
22-
~~~~~~~~
2320
!!! error TS2554: Expected 0 arguments, but got 1.
2421

2522
declare class C { constructor(value: number); }

tests/baselines/reference/callOverloads1.errors.txt

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
tests/cases/compiler/callOverloads1.ts(1,7): error TS2300: Duplicate identifier 'Foo'.
22
tests/cases/compiler/callOverloads1.ts(9,10): error TS2300: Duplicate identifier 'Foo'.
33
tests/cases/compiler/callOverloads1.ts(9,10): error TS2391: Function implementation is missing or not immediately following the declaration.
4-
tests/cases/compiler/callOverloads1.ts(13,10): error TS2350: Only a void function can be called with the 'new' keyword.
54
tests/cases/compiler/callOverloads1.ts(13,10): error TS2554: Expected 0 arguments, but got 1.
65

76

8-
==== tests/cases/compiler/callOverloads1.ts (5 errors) ====
7+
==== tests/cases/compiler/callOverloads1.ts (4 errors) ====
98
class Foo { // error
109
~~~
1110
!!! error TS2300: Duplicate identifier 'Foo'.
@@ -26,8 +25,6 @@ tests/cases/compiler/callOverloads1.ts(13,10): error TS2554: Expected 0 argument
2625

2726
var f1 = new Foo("hey");
2827
~~~~~~~~~~~~~~
29-
!!! error TS2350: Only a void function can be called with the 'new' keyword.
30-
~~~~~~~~~~~~~~
3128
!!! error TS2554: Expected 0 arguments, but got 1.
3229

3330

tests/baselines/reference/callOverloads2.errors.txt

+1-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ tests/cases/compiler/callOverloads2.ts(11,10): error TS2389: Function implementa
44
tests/cases/compiler/callOverloads2.ts(11,10): error TS2393: Duplicate function implementation.
55
tests/cases/compiler/callOverloads2.ts(12,10): error TS2393: Duplicate function implementation.
66
tests/cases/compiler/callOverloads2.ts(14,10): error TS2391: Function implementation is missing or not immediately following the declaration.
7-
tests/cases/compiler/callOverloads2.ts(18,10): error TS2350: Only a void function can be called with the 'new' keyword.
87
tests/cases/compiler/callOverloads2.ts(18,10): error TS2554: Expected 0 arguments, but got 1.
98

109

11-
==== tests/cases/compiler/callOverloads2.ts (8 errors) ====
10+
==== tests/cases/compiler/callOverloads2.ts (7 errors) ====
1211
class Foo { // error
1312
~~~
1413
!!! error TS2300: Duplicate identifier 'Foo'.
@@ -40,8 +39,6 @@ tests/cases/compiler/callOverloads2.ts(18,10): error TS2554: Expected 0 argument
4039

4140
var f1 = new Foo("hey");
4241
~~~~~~~~~~~~~~
43-
!!! error TS2350: Only a void function can be called with the 'new' keyword.
44-
~~~~~~~~~~~~~~
4542
!!! error TS2554: Expected 0 arguments, but got 1.
4643

4744

tests/baselines/reference/constructorFunctions.errors.txt

+13-1
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

+13
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

+14
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

+1-7
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/baselines/reference/overloadsAndTypeArgumentArityErrors.errors.txt

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
tests/cases/compiler/overloadsAndTypeArgumentArityErrors.ts(5,11): error TS2558: Expected 0-2 type arguments, but got 3.
2-
tests/cases/compiler/overloadsAndTypeArgumentArityErrors.ts(6,1): error TS2350: Only a void function can be called with the 'new' keyword.
32
tests/cases/compiler/overloadsAndTypeArgumentArityErrors.ts(6,15): error TS2558: Expected 0-2 type arguments, but got 3.
43
tests/cases/compiler/overloadsAndTypeArgumentArityErrors.ts(9,1): error TS2554: Expected 1 arguments, but got 0.
54

65

7-
==== tests/cases/compiler/overloadsAndTypeArgumentArityErrors.ts (4 errors) ====
6+
==== tests/cases/compiler/overloadsAndTypeArgumentArityErrors.ts (3 errors) ====
87
declare function Callbacks(flags?: string): void;
98
declare function Callbacks<T>(flags?: string): void;
109
declare function Callbacks<T1, T2>(flags?: string): void;
@@ -13,8 +12,6 @@ tests/cases/compiler/overloadsAndTypeArgumentArityErrors.ts(9,1): error TS2554:
1312
~~~~~~~~~~~~~~~~~~~~~~~
1413
!!! error TS2558: Expected 0-2 type arguments, but got 3.
1514
new Callbacks<number, string, boolean>('s'); // wrong number of type arguments
16-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17-
!!! error TS2350: Only a void function can be called with the 'new' keyword.
1815
~~~~~~~~~~~~~~~~~~~~~~~
1916
!!! error TS2558: Expected 0-2 type arguments, but got 3.
2017

tests/cases/conformance/salsa/constructorFunctions.ts

+9
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)