From 94848036508ed03901d715b954c3342c61f6cee3 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 21 Aug 2018 09:19:29 -0700 Subject: [PATCH] When --noImplicitAny is enabled, don't report errors suggesting that a 'void' function can be 'new'ed --- src/compiler/checker.ts | 12 +++++---- ...peratorErrorCases_noImplicitAny.errors.txt | 21 ++++++++++++++++ .../newOperatorErrorCases_noImplicitAny.js | 18 +++++++++++++ ...ewOperatorErrorCases_noImplicitAny.symbols | 21 ++++++++++++++++ .../newOperatorErrorCases_noImplicitAny.types | 25 +++++++++++++++++++ .../newOperatorErrorCases_noImplicitAny.ts | 10 ++++++++ 6 files changed, 102 insertions(+), 5 deletions(-) create mode 100644 tests/baselines/reference/newOperatorErrorCases_noImplicitAny.errors.txt create mode 100644 tests/baselines/reference/newOperatorErrorCases_noImplicitAny.js create mode 100644 tests/baselines/reference/newOperatorErrorCases_noImplicitAny.symbols create mode 100644 tests/baselines/reference/newOperatorErrorCases_noImplicitAny.types create mode 100644 tests/cases/conformance/expressions/newOperator/newOperatorErrorCases_noImplicitAny.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 0b357bd771da4..7fec31477c184 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -19458,11 +19458,13 @@ namespace ts { const callSignatures = getSignaturesOfType(expressionType, SignatureKind.Call); if (callSignatures.length) { const signature = resolveCall(node, callSignatures, candidatesOutArray, isForSignatureHelp); - 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) { - error(node, Diagnostics.A_function_that_is_called_with_the_new_keyword_cannot_have_a_this_type_that_is_void); + if (!noImplicitAny) { + 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) { + error(node, Diagnostics.A_function_that_is_called_with_the_new_keyword_cannot_have_a_this_type_that_is_void); + } } return signature; } diff --git a/tests/baselines/reference/newOperatorErrorCases_noImplicitAny.errors.txt b/tests/baselines/reference/newOperatorErrorCases_noImplicitAny.errors.txt new file mode 100644 index 0000000000000..3c131f4136f23 --- /dev/null +++ b/tests/baselines/reference/newOperatorErrorCases_noImplicitAny.errors.txt @@ -0,0 +1,21 @@ +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. +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. +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. + + +==== tests/cases/conformance/expressions/newOperator/newOperatorErrorCases_noImplicitAny.ts (3 errors) ==== + function fnNumber(this: void): number { return 90; } + new fnNumber(); // Error + ~~~~~~~~~~~~~~ +!!! error TS7009: 'new' expression, whose target lacks a construct signature, implicitly has an 'any' type. + + function fnVoid(this: void): void {} + new fnVoid(); // Error + ~~~~~~~~~~~~ +!!! error TS7009: 'new' expression, whose target lacks a construct signature, implicitly has an 'any' type. + + function functionVoidNoThis(): void {} + new functionVoidNoThis(); // Error + ~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS7009: 'new' expression, whose target lacks a construct signature, implicitly has an 'any' type. + \ No newline at end of file diff --git a/tests/baselines/reference/newOperatorErrorCases_noImplicitAny.js b/tests/baselines/reference/newOperatorErrorCases_noImplicitAny.js new file mode 100644 index 0000000000000..935c2ba036f51 --- /dev/null +++ b/tests/baselines/reference/newOperatorErrorCases_noImplicitAny.js @@ -0,0 +1,18 @@ +//// [newOperatorErrorCases_noImplicitAny.ts] +function fnNumber(this: void): number { return 90; } +new fnNumber(); // Error + +function fnVoid(this: void): void {} +new fnVoid(); // Error + +function functionVoidNoThis(): void {} +new functionVoidNoThis(); // Error + + +//// [newOperatorErrorCases_noImplicitAny.js] +function fnNumber() { return 90; } +new fnNumber(); // Error +function fnVoid() { } +new fnVoid(); // Error +function functionVoidNoThis() { } +new functionVoidNoThis(); // Error diff --git a/tests/baselines/reference/newOperatorErrorCases_noImplicitAny.symbols b/tests/baselines/reference/newOperatorErrorCases_noImplicitAny.symbols new file mode 100644 index 0000000000000..dadb237922345 --- /dev/null +++ b/tests/baselines/reference/newOperatorErrorCases_noImplicitAny.symbols @@ -0,0 +1,21 @@ +=== tests/cases/conformance/expressions/newOperator/newOperatorErrorCases_noImplicitAny.ts === +function fnNumber(this: void): number { return 90; } +>fnNumber : Symbol(fnNumber, Decl(newOperatorErrorCases_noImplicitAny.ts, 0, 0)) +>this : Symbol(this, Decl(newOperatorErrorCases_noImplicitAny.ts, 0, 18)) + +new fnNumber(); // Error +>fnNumber : Symbol(fnNumber, Decl(newOperatorErrorCases_noImplicitAny.ts, 0, 0)) + +function fnVoid(this: void): void {} +>fnVoid : Symbol(fnVoid, Decl(newOperatorErrorCases_noImplicitAny.ts, 1, 15)) +>this : Symbol(this, Decl(newOperatorErrorCases_noImplicitAny.ts, 3, 16)) + +new fnVoid(); // Error +>fnVoid : Symbol(fnVoid, Decl(newOperatorErrorCases_noImplicitAny.ts, 1, 15)) + +function functionVoidNoThis(): void {} +>functionVoidNoThis : Symbol(functionVoidNoThis, Decl(newOperatorErrorCases_noImplicitAny.ts, 4, 13)) + +new functionVoidNoThis(); // Error +>functionVoidNoThis : Symbol(functionVoidNoThis, Decl(newOperatorErrorCases_noImplicitAny.ts, 4, 13)) + diff --git a/tests/baselines/reference/newOperatorErrorCases_noImplicitAny.types b/tests/baselines/reference/newOperatorErrorCases_noImplicitAny.types new file mode 100644 index 0000000000000..bd1d8082aae40 --- /dev/null +++ b/tests/baselines/reference/newOperatorErrorCases_noImplicitAny.types @@ -0,0 +1,25 @@ +=== tests/cases/conformance/expressions/newOperator/newOperatorErrorCases_noImplicitAny.ts === +function fnNumber(this: void): number { return 90; } +>fnNumber : (this: void) => number +>this : void +>90 : 90 + +new fnNumber(); // Error +>new fnNumber() : any +>fnNumber : (this: void) => number + +function fnVoid(this: void): void {} +>fnVoid : (this: void) => void +>this : void + +new fnVoid(); // Error +>new fnVoid() : any +>fnVoid : (this: void) => void + +function functionVoidNoThis(): void {} +>functionVoidNoThis : () => void + +new functionVoidNoThis(); // Error +>new functionVoidNoThis() : any +>functionVoidNoThis : () => void + diff --git a/tests/cases/conformance/expressions/newOperator/newOperatorErrorCases_noImplicitAny.ts b/tests/cases/conformance/expressions/newOperator/newOperatorErrorCases_noImplicitAny.ts new file mode 100644 index 0000000000000..9b3b2c091d655 --- /dev/null +++ b/tests/cases/conformance/expressions/newOperator/newOperatorErrorCases_noImplicitAny.ts @@ -0,0 +1,10 @@ +// @noImplicitAny: true + +function fnNumber(this: void): number { return 90; } +new fnNumber(); // Error + +function fnVoid(this: void): void {} +new fnVoid(); // Error + +function functionVoidNoThis(): void {} +new functionVoidNoThis(); // Error