Skip to content

Commit 7288651

Browse files
author
Andy
authored
When --noImplicitAny is enabled, don't report errors suggesting that a 'void' function can be 'new'ed (#26579)
1 parent 42c9208 commit 7288651

6 files changed

+102
-5
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19458,11 +19458,13 @@ namespace ts {
1945819458
const callSignatures = getSignaturesOfType(expressionType, SignatureKind.Call);
1945919459
if (callSignatures.length) {
1946019460
const signature = resolveCall(node, callSignatures, candidatesOutArray, isForSignatureHelp);
19461-
if (signature.declaration && !isJavascriptConstructor(signature.declaration) && getReturnTypeOfSignature(signature) !== voidType) {
19462-
error(node, Diagnostics.Only_a_void_function_can_be_called_with_the_new_keyword);
19463-
}
19464-
if (getThisTypeOfSignature(signature) === voidType) {
19465-
error(node, Diagnostics.A_function_that_is_called_with_the_new_keyword_cannot_have_a_this_type_that_is_void);
19461+
if (!noImplicitAny) {
19462+
if (signature.declaration && !isJavascriptConstructor(signature.declaration) && getReturnTypeOfSignature(signature) !== voidType) {
19463+
error(node, Diagnostics.Only_a_void_function_can_be_called_with_the_new_keyword);
19464+
}
19465+
if (getThisTypeOfSignature(signature) === voidType) {
19466+
error(node, Diagnostics.A_function_that_is_called_with_the_new_keyword_cannot_have_a_this_type_that_is_void);
19467+
}
1946619468
}
1946719469
return signature;
1946819470
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
tests/cases/conformance/expressions/newOperator/newOperatorErrorCases_noImplicitAny.ts(2,1): error TS7009: 'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.
2+
tests/cases/conformance/expressions/newOperator/newOperatorErrorCases_noImplicitAny.ts(5,1): error TS7009: 'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.
3+
tests/cases/conformance/expressions/newOperator/newOperatorErrorCases_noImplicitAny.ts(8,1): error TS7009: 'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.
4+
5+
6+
==== tests/cases/conformance/expressions/newOperator/newOperatorErrorCases_noImplicitAny.ts (3 errors) ====
7+
function fnNumber(this: void): number { return 90; }
8+
new fnNumber(); // Error
9+
~~~~~~~~~~~~~~
10+
!!! error TS7009: 'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.
11+
12+
function fnVoid(this: void): void {}
13+
new fnVoid(); // Error
14+
~~~~~~~~~~~~
15+
!!! error TS7009: 'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.
16+
17+
function functionVoidNoThis(): void {}
18+
new functionVoidNoThis(); // Error
19+
~~~~~~~~~~~~~~~~~~~~~~~~
20+
!!! error TS7009: 'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.
21+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [newOperatorErrorCases_noImplicitAny.ts]
2+
function fnNumber(this: void): number { return 90; }
3+
new fnNumber(); // Error
4+
5+
function fnVoid(this: void): void {}
6+
new fnVoid(); // Error
7+
8+
function functionVoidNoThis(): void {}
9+
new functionVoidNoThis(); // Error
10+
11+
12+
//// [newOperatorErrorCases_noImplicitAny.js]
13+
function fnNumber() { return 90; }
14+
new fnNumber(); // Error
15+
function fnVoid() { }
16+
new fnVoid(); // Error
17+
function functionVoidNoThis() { }
18+
new functionVoidNoThis(); // Error
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
=== tests/cases/conformance/expressions/newOperator/newOperatorErrorCases_noImplicitAny.ts ===
2+
function fnNumber(this: void): number { return 90; }
3+
>fnNumber : Symbol(fnNumber, Decl(newOperatorErrorCases_noImplicitAny.ts, 0, 0))
4+
>this : Symbol(this, Decl(newOperatorErrorCases_noImplicitAny.ts, 0, 18))
5+
6+
new fnNumber(); // Error
7+
>fnNumber : Symbol(fnNumber, Decl(newOperatorErrorCases_noImplicitAny.ts, 0, 0))
8+
9+
function fnVoid(this: void): void {}
10+
>fnVoid : Symbol(fnVoid, Decl(newOperatorErrorCases_noImplicitAny.ts, 1, 15))
11+
>this : Symbol(this, Decl(newOperatorErrorCases_noImplicitAny.ts, 3, 16))
12+
13+
new fnVoid(); // Error
14+
>fnVoid : Symbol(fnVoid, Decl(newOperatorErrorCases_noImplicitAny.ts, 1, 15))
15+
16+
function functionVoidNoThis(): void {}
17+
>functionVoidNoThis : Symbol(functionVoidNoThis, Decl(newOperatorErrorCases_noImplicitAny.ts, 4, 13))
18+
19+
new functionVoidNoThis(); // Error
20+
>functionVoidNoThis : Symbol(functionVoidNoThis, Decl(newOperatorErrorCases_noImplicitAny.ts, 4, 13))
21+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/conformance/expressions/newOperator/newOperatorErrorCases_noImplicitAny.ts ===
2+
function fnNumber(this: void): number { return 90; }
3+
>fnNumber : (this: void) => number
4+
>this : void
5+
>90 : 90
6+
7+
new fnNumber(); // Error
8+
>new fnNumber() : any
9+
>fnNumber : (this: void) => number
10+
11+
function fnVoid(this: void): void {}
12+
>fnVoid : (this: void) => void
13+
>this : void
14+
15+
new fnVoid(); // Error
16+
>new fnVoid() : any
17+
>fnVoid : (this: void) => void
18+
19+
function functionVoidNoThis(): void {}
20+
>functionVoidNoThis : () => void
21+
22+
new functionVoidNoThis(); // Error
23+
>new functionVoidNoThis() : any
24+
>functionVoidNoThis : () => void
25+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @noImplicitAny: true
2+
3+
function fnNumber(this: void): number { return 90; }
4+
new fnNumber(); // Error
5+
6+
function fnVoid(this: void): void {}
7+
new fnVoid(); // Error
8+
9+
function functionVoidNoThis(): void {}
10+
new functionVoidNoThis(); // Error

0 commit comments

Comments
 (0)