Skip to content

Commit b017c6d

Browse files
Andymheiber
Andy
authored andcommitted
When --noImplicitAny is enabled, don't report errors suggesting that a 'void' function can be 'new'ed (microsoft#26579)
1 parent 9bdc149 commit b017c6d

File tree

6 files changed

+102
-5
lines changed

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
@@ -19480,11 +19480,13 @@ namespace ts {
1948019480
const callSignatures = getSignaturesOfType(expressionType, SignatureKind.Call);
1948119481
if (callSignatures.length) {
1948219482
const signature = resolveCall(node, callSignatures, candidatesOutArray, isForSignatureHelp);
19483-
if (signature.declaration && !isJavascriptConstructor(signature.declaration) && getReturnTypeOfSignature(signature) !== voidType) {
19484-
error(node, Diagnostics.Only_a_void_function_can_be_called_with_the_new_keyword);
19485-
}
19486-
if (getThisTypeOfSignature(signature) === voidType) {
19487-
error(node, Diagnostics.A_function_that_is_called_with_the_new_keyword_cannot_have_a_this_type_that_is_void);
19483+
if (!noImplicitAny) {
19484+
if (signature.declaration && !isJavascriptConstructor(signature.declaration) && getReturnTypeOfSignature(signature) !== voidType) {
19485+
error(node, Diagnostics.Only_a_void_function_can_be_called_with_the_new_keyword);
19486+
}
19487+
if (getThisTypeOfSignature(signature) === voidType) {
19488+
error(node, Diagnostics.A_function_that_is_called_with_the_new_keyword_cannot_have_a_this_type_that_is_void);
19489+
}
1948819490
}
1948919491
return signature;
1949019492
}
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)