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.
This commit is contained in:
James Keane 2018-08-01 16:40:55 -04:00 committed by Nathan Shively-Sanders
parent a5a26ec939
commit 50f442f9ff
10 changed files with 55 additions and 25 deletions

View file

@ -19592,7 +19592,7 @@ namespace ts {
const callSignatures = getSignaturesOfType(expressionType, SignatureKind.Call);
if (callSignatures.length) {
const signature = resolveCall(node, callSignatures, candidatesOutArray, isForSignatureHelp);
if (!isJavaScriptConstructor(signature.declaration) && getReturnTypeOfSignature(signature) !== voidType) {
if (signature.declaration && !isJavaScriptConstructor(signature.declaration) && getReturnTypeOfSignature(signature) !== voidType) {
error(node, Diagnostics.Only_a_void_function_can_be_called_with_the_new_keyword);
}
if (getThisTypeOfSignature(signature) === voidType) {

View file

@ -1,11 +1,10 @@
tests/cases/compiler/callOnInstance.ts(1,18): error TS2300: Duplicate identifier 'D'.
tests/cases/compiler/callOnInstance.ts(3,15): error TS2300: Duplicate identifier 'D'.
tests/cases/compiler/callOnInstance.ts(7,19): error TS2350: Only a void function can be called with the 'new' keyword.
tests/cases/compiler/callOnInstance.ts(7,19): error TS2554: Expected 0 arguments, but got 1.
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.
==== tests/cases/compiler/callOnInstance.ts (5 errors) ====
==== tests/cases/compiler/callOnInstance.ts (4 errors) ====
declare function D(): string; // error
~
!!! error TS2300: Duplicate identifier 'D'.
@ -18,8 +17,6 @@ tests/cases/compiler/callOnInstance.ts(10,1): error TS2349: Cannot invoke an exp
var s2: string = (new D(1))();
~~~~~~~~
!!! error TS2350: Only a void function can be called with the 'new' keyword.
~~~~~~~~
!!! error TS2554: Expected 0 arguments, but got 1.
declare class C { constructor(value: number); }

View file

@ -1,11 +1,10 @@
tests/cases/compiler/callOverloads1.ts(1,7): error TS2300: Duplicate identifier 'Foo'.
tests/cases/compiler/callOverloads1.ts(9,10): error TS2300: Duplicate identifier 'Foo'.
tests/cases/compiler/callOverloads1.ts(9,10): error TS2391: Function implementation is missing or not immediately following the declaration.
tests/cases/compiler/callOverloads1.ts(13,10): error TS2350: Only a void function can be called with the 'new' keyword.
tests/cases/compiler/callOverloads1.ts(13,10): error TS2554: Expected 0 arguments, but got 1.
==== tests/cases/compiler/callOverloads1.ts (5 errors) ====
==== tests/cases/compiler/callOverloads1.ts (4 errors) ====
class Foo { // error
~~~
!!! error TS2300: Duplicate identifier 'Foo'.
@ -26,8 +25,6 @@ tests/cases/compiler/callOverloads1.ts(13,10): error TS2554: Expected 0 argument
var f1 = new Foo("hey");
~~~~~~~~~~~~~~
!!! error TS2350: Only a void function can be called with the 'new' keyword.
~~~~~~~~~~~~~~
!!! error TS2554: Expected 0 arguments, but got 1.

View file

@ -4,11 +4,10 @@ tests/cases/compiler/callOverloads2.ts(11,10): error TS2389: Function implementa
tests/cases/compiler/callOverloads2.ts(11,10): error TS2393: Duplicate function implementation.
tests/cases/compiler/callOverloads2.ts(12,10): error TS2393: Duplicate function implementation.
tests/cases/compiler/callOverloads2.ts(14,10): error TS2391: Function implementation is missing or not immediately following the declaration.
tests/cases/compiler/callOverloads2.ts(18,10): error TS2350: Only a void function can be called with the 'new' keyword.
tests/cases/compiler/callOverloads2.ts(18,10): error TS2554: Expected 0 arguments, but got 1.
==== tests/cases/compiler/callOverloads2.ts (8 errors) ====
==== tests/cases/compiler/callOverloads2.ts (7 errors) ====
class Foo { // error
~~~
!!! error TS2300: Duplicate identifier 'Foo'.
@ -40,8 +39,6 @@ tests/cases/compiler/callOverloads2.ts(18,10): error TS2554: Expected 0 argument
var f1 = new Foo("hey");
~~~~~~~~~~~~~~
!!! error TS2350: Only a void function can be called with the 'new' keyword.
~~~~~~~~~~~~~~
!!! error TS2554: Expected 0 arguments, but got 1.

View file

@ -1,8 +1,9 @@
tests/cases/conformance/salsa/index.js(22,15): error TS2348: Value of type 'typeof C3' is not callable. Did you mean to include 'new'?
tests/cases/conformance/salsa/index.js(30,15): error TS2348: Value of type 'typeof C4' is not callable. Did you mean to include 'new'?
tests/cases/conformance/salsa/index.js(55,13): error TS2554: Expected 1 arguments, but got 0.
==== tests/cases/conformance/salsa/index.js (2 errors) ====
==== tests/cases/conformance/salsa/index.js (3 errors) ====
function C1() {
if (!(this instanceof C1)) return new C1();
this.x = 1;
@ -53,4 +54,15 @@ tests/cases/conformance/salsa/index.js(30,15): error TS2348: Value of type 'type
};
var c6_v1 = new C6();
/**
* @constructor
* @param {number} num
*/
function C7(num) {}
var c7_v1 = new C7();
~~~~~~~~
!!! error TS2554: Expected 1 arguments, but got 0.

View file

@ -116,3 +116,16 @@ var c6_v1 = new C6();
>c6_v1 : Symbol(c6_v1, Decl(index.js, 45, 3))
>C6 : Symbol(C6, Decl(index.js, 38, 12))
/**
* @constructor
* @param {number} num
*/
function C7(num) {}
>C7 : Symbol(C7, Decl(index.js, 45, 21))
>num : Symbol(num, Decl(index.js, 52, 12))
var c7_v1 = new C7();
>c7_v1 : Symbol(c7_v1, Decl(index.js, 54, 3))
>C7 : Symbol(C7, Decl(index.js, 45, 21))

View file

@ -168,3 +168,17 @@ var c6_v1 = new C6();
>new C6() : C6
>C6 : typeof C6
/**
* @constructor
* @param {number} num
*/
function C7(num) {}
>C7 : typeof C7
>num : number
var c7_v1 = new C7();
>c7_v1 : any
>new C7() : any
>C7 : typeof C7

View file

@ -1,12 +1,10 @@
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiateNonGenericTypeWithTypeArguments.ts(8,15): error TS2558: Expected 0 type arguments, but got 1.
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiateNonGenericTypeWithTypeArguments.ts(11,9): error TS2350: Only a void function can be called with the 'new' keyword.
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiateNonGenericTypeWithTypeArguments.ts(11,17): error TS2558: Expected 0 type arguments, but got 1.
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiateNonGenericTypeWithTypeArguments.ts(14,10): error TS2350: Only a void function can be called with the 'new' keyword.
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiateNonGenericTypeWithTypeArguments.ts(14,16): error TS2558: Expected 0 type arguments, but got 1.
tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiateNonGenericTypeWithTypeArguments.ts(18,10): error TS2347: Untyped function calls may not accept type arguments.
==== tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiateNonGenericTypeWithTypeArguments.ts (6 errors) ====
==== tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiateNonGenericTypeWithTypeArguments.ts (4 errors) ====
// it is an error to provide type arguments to a non-generic call
// all of these are errors
@ -20,15 +18,11 @@ tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiateNonGen
function Foo(): void { }
var r = new Foo<number>();
~~~~~~~~~~~~~~~~~
!!! error TS2350: Only a void function can be called with the 'new' keyword.
~~~~~~
!!! error TS2558: Expected 0 type arguments, but got 1.
var f: { (): void };
var r2 = new f<number>();
~~~~~~~~~~~~~~~
!!! error TS2350: Only a void function can be called with the 'new' keyword.
~~~~~~
!!! error TS2558: Expected 0 type arguments, but got 1.

View file

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

View file

@ -49,3 +49,12 @@ function C6() {
};
var c6_v1 = new C6();
/**
* @constructor
* @param {number} num
*/
function C7(num) {}
var c7_v1 = new C7();