Skip to content

Elaborate type of invoked expression in error message #5784

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ internal/
.settings
.vscode/*
!.vscode/tasks.json
.idea
11 changes: 8 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3854,6 +3854,11 @@ namespace ts {
return false;
}

function typeHasCallSignatures(type: Type): boolean {
const callSignature = getSignaturesOfType(type, SignatureKind.Call);
return !!callSignature.length;
}

function typeHasCallOrConstructSignatures(type: Type): boolean {
const apparentType = getApparentType(type);
if (apparentType.flags & TypeFlags.StructuredType) {
Expand Down Expand Up @@ -9496,7 +9501,7 @@ namespace ts {
error(node, Diagnostics.Value_of_type_0_is_not_callable_Did_you_mean_to_include_new, typeToString(funcType));
}
else {
error(node, Diagnostics.Cannot_invoke_an_expression_whose_type_lacks_a_call_signature);
error(node, Diagnostics.Cannot_invoke_an_expression_whose_type_lacks_a_call_signature_Type_0_has_no_compatible_call_signatures, typeToString(apparentType));
}
return resolveErrorCall(node);
}
Expand Down Expand Up @@ -9586,7 +9591,7 @@ namespace ts {
}

if (!callSignatures.length) {
error(node, Diagnostics.Cannot_invoke_an_expression_whose_type_lacks_a_call_signature);
error(node, Diagnostics.Cannot_invoke_an_expression_whose_type_lacks_a_call_signature_Type_0_has_no_compatible_call_signatures, typeToString(apparentType));
return resolveErrorCall(node);
}

Expand Down Expand Up @@ -9633,7 +9638,7 @@ namespace ts {
const headMessage = getDiagnosticHeadMessageForDecoratorResolution(node);
if (!callSignatures.length) {
let errorInfo: DiagnosticMessageChain;
errorInfo = chainDiagnosticMessages(errorInfo, Diagnostics.Cannot_invoke_an_expression_whose_type_lacks_a_call_signature);
errorInfo = chainDiagnosticMessages(errorInfo, Diagnostics.Cannot_invoke_an_expression_whose_type_lacks_a_call_signature_Type_0_has_no_compatible_call_signatures, typeToString(apparentType));
errorInfo = chainDiagnosticMessages(errorInfo, headMessage);
diagnostics.add(createDiagnosticForNodeFromMessageChain(node, errorInfo));
return resolveErrorCall(node);
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@
"category": "Error",
"code": 2348
},
"Cannot invoke an expression whose type lacks a call signature.": {
"Cannot invoke an expression whose type lacks a call signature. Type '{0}' has no compatible call signatures.": {
"category": "Error",
"code": 2349
},
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/callOnInstance.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ tests/cases/compiler/callOnInstance.ts(1,18): error TS2300: Duplicate identifier
tests/cases/compiler/callOnInstance.ts(3,15): error TS2300: Duplicate identifier 'D'.
tests/cases/compiler/callOnInstance.ts(7,19): error TS2346: Supplied parameters do not match any signature of call target.
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(10,1): error TS2349: Cannot invoke an expression whose type lacks a call signature.
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) ====
Expand All @@ -25,4 +25,4 @@ tests/cases/compiler/callOnInstance.ts(10,1): error TS2349: Cannot invoke an exp
declare class C { constructor(value: number); }
(new C(1))(); // Error for calling an instance
~~~~~~~~~~~~
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature.
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'C' has no compatible call signatures.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tests/cases/compiler/computedPropertiesInDestructuring1.ts(20,8): error TS2349: Cannot invoke an expression whose type lacks a call signature.
tests/cases/compiler/computedPropertiesInDestructuring1.ts(20,8): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
tests/cases/compiler/computedPropertiesInDestructuring1.ts(21,12): error TS2339: Property 'toExponential' does not exist on type 'string'.
tests/cases/compiler/computedPropertiesInDestructuring1.ts(33,4): error TS2349: Cannot invoke an expression whose type lacks a call signature.
tests/cases/compiler/computedPropertiesInDestructuring1.ts(33,4): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
tests/cases/compiler/computedPropertiesInDestructuring1.ts(34,5): error TS2365: Operator '+' cannot be applied to types 'number' and '{}'.


Expand All @@ -26,7 +26,7 @@ tests/cases/compiler/computedPropertiesInDestructuring1.ts(34,5): error TS2365:
// report errors on type errors in computed properties used in destructuring
let [{[foo()]: bar6}] = [{bar: "bar"}];
~~~~~
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature.
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
let [{[foo.toExponential()]: bar7}] = [{bar: "bar"}];
~~~~~~~~~~~~~
!!! error TS2339: Property 'toExponential' does not exist on type 'string'.
Expand All @@ -43,7 +43,7 @@ tests/cases/compiler/computedPropertiesInDestructuring1.ts(34,5): error TS2365:

[{[foo()]: bar4}] = [{bar: "bar"}];
~~~~~
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature.
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
[{[(1 + {})]: bar4}] = [{bar: "bar"}];
~~~~~~
!!! error TS2365: Operator '+' cannot be applied to types 'number' and '{}'.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts(21,8): error TS2349: Cannot invoke an expression whose type lacks a call signature.
tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts(21,8): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts(22,12): error TS2339: Property 'toExponential' does not exist on type 'string'.
tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts(34,4): error TS2349: Cannot invoke an expression whose type lacks a call signature.
tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts(34,4): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts(35,5): error TS2365: Operator '+' cannot be applied to types 'number' and '{}'.


Expand All @@ -27,7 +27,7 @@ tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts(35,5): error TS23
// report errors on type errors in computed properties used in destructuring
let [{[foo()]: bar6}] = [{bar: "bar"}];
~~~~~
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature.
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
let [{[foo.toExponential()]: bar7}] = [{bar: "bar"}];
~~~~~~~~~~~~~
!!! error TS2339: Property 'toExponential' does not exist on type 'string'.
Expand All @@ -44,7 +44,7 @@ tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts(35,5): error TS23

[{[foo()]: bar4}] = [{bar: "bar"}];
~~~~~
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature.
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
[{[(1 + {})]: bar4}] = [{bar: "bar"}];
~~~~~~
!!! error TS2365: Operator '+' cannot be applied to types 'number' and '{}'.
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/constructorOverloads4.errors.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
tests/cases/compiler/constructorOverloads4.ts(2,18): error TS2300: Duplicate identifier 'Function'.
tests/cases/compiler/constructorOverloads4.ts(5,21): error TS2300: Duplicate identifier 'Function'.
tests/cases/compiler/constructorOverloads4.ts(6,21): error TS2300: Duplicate identifier 'Function'.
tests/cases/compiler/constructorOverloads4.ts(10,1): error TS2349: Cannot invoke an expression whose type lacks a call signature.
tests/cases/compiler/constructorOverloads4.ts(10,1): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'Function' has no compatible call signatures.
tests/cases/compiler/constructorOverloads4.ts(11,1): error TS2348: Value of type 'typeof Function' is not callable. Did you mean to include 'new'?


Expand All @@ -23,7 +23,7 @@ tests/cases/compiler/constructorOverloads4.ts(11,1): error TS2348: Value of type

(new M.Function("return 5"))();
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature.
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'Function' has no compatible call signatures.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a regression

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifically, we're not looking for call signatures here. We're looking for a construct signature, so we're not appropriately reporting an error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain the semantics here? I don't think that class Function and function Function will merge (hence the duplicate identifier errors), so the error that changes should be based on only one of the definitions. The first definition has no call signatures, so if the error is based on the first definition, then it looks correct to me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must have misread the error span.

M.Function("yo");
~~~~~~~~~~~~~~~~
!!! error TS2348: Value of type 'typeof Function' is not callable. Did you mean to include 'new'?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/compiler/functionExpressionShadowedByParams.ts(3,4): error TS2349: Cannot invoke an expression whose type lacks a call signature.
tests/cases/compiler/functionExpressionShadowedByParams.ts(3,4): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'Number' has no compatible call signatures.
tests/cases/compiler/functionExpressionShadowedByParams.ts(10,9): error TS2339: Property 'apply' does not exist on type 'number'.


Expand All @@ -7,7 +7,7 @@ tests/cases/compiler/functionExpressionShadowedByParams.ts(10,9): error TS2339:
b1.toPrecision(2); // should not error
b1(12); // should error
~~~~~~
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature.
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'Number' has no compatible call signatures.
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
tests/cases/conformance/classes/members/classTypes/instancePropertiesInheritedIntoClassType.ts(4,13): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
tests/cases/conformance/classes/members/classTypes/instancePropertiesInheritedIntoClassType.ts(7,13): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
tests/cases/conformance/classes/members/classTypes/instancePropertiesInheritedIntoClassType.ts(19,14): error TS2349: Cannot invoke an expression whose type lacks a call signature.
tests/cases/conformance/classes/members/classTypes/instancePropertiesInheritedIntoClassType.ts(19,14): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'Number' has no compatible call signatures.
tests/cases/conformance/classes/members/classTypes/instancePropertiesInheritedIntoClassType.ts(26,13): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
tests/cases/conformance/classes/members/classTypes/instancePropertiesInheritedIntoClassType.ts(29,13): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
tests/cases/conformance/classes/members/classTypes/instancePropertiesInheritedIntoClassType.ts(41,14): error TS2349: Cannot invoke an expression whose type lacks a call signature.
tests/cases/conformance/classes/members/classTypes/instancePropertiesInheritedIntoClassType.ts(41,14): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.


==== tests/cases/conformance/classes/members/classTypes/instancePropertiesInheritedIntoClassType.ts (6 errors) ====
Expand Down Expand Up @@ -31,7 +31,7 @@ tests/cases/conformance/classes/members/classTypes/instancePropertiesInheritedIn
r.y = 4;
var r6 = d.y(); // error
~~~~~
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature.
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'Number' has no compatible call signatures.

}

Expand Down Expand Up @@ -59,5 +59,5 @@ tests/cases/conformance/classes/members/classTypes/instancePropertiesInheritedIn
r.y = '';
var r6 = d.y(); // error
~~~~~
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature.
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
tests/cases/conformance/classes/members/classTypes/instancePropertyInClassType.ts(4,13): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
tests/cases/conformance/classes/members/classTypes/instancePropertyInClassType.ts(7,13): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
tests/cases/conformance/classes/members/classTypes/instancePropertyInClassType.ts(17,14): error TS2349: Cannot invoke an expression whose type lacks a call signature.
tests/cases/conformance/classes/members/classTypes/instancePropertyInClassType.ts(17,14): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'Number' has no compatible call signatures.
tests/cases/conformance/classes/members/classTypes/instancePropertyInClassType.ts(24,13): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
tests/cases/conformance/classes/members/classTypes/instancePropertyInClassType.ts(27,13): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
tests/cases/conformance/classes/members/classTypes/instancePropertyInClassType.ts(37,14): error TS2349: Cannot invoke an expression whose type lacks a call signature.
tests/cases/conformance/classes/members/classTypes/instancePropertyInClassType.ts(37,14): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.


==== tests/cases/conformance/classes/members/classTypes/instancePropertyInClassType.ts (6 errors) ====
Expand All @@ -29,7 +29,7 @@ tests/cases/conformance/classes/members/classTypes/instancePropertyInClassType.t
r.y = 4;
var r6 = c.y(); // error
~~~~~
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature.
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'Number' has no compatible call signatures.

}

Expand All @@ -55,5 +55,5 @@ tests/cases/conformance/classes/members/classTypes/instancePropertyInClassType.t
r.y = '';
var r6 = c.y(); // error
~~~~~
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature.
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
}
4 changes: 2 additions & 2 deletions tests/baselines/reference/strictModeReservedWord.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ tests/cases/compiler/strictModeReservedWord.ts(22,22): error TS1212: Identifier
tests/cases/compiler/strictModeReservedWord.ts(22,30): error TS1212: Identifier expected. 'implements' is a reserved word in strict mode
tests/cases/compiler/strictModeReservedWord.ts(23,5): error TS2304: Cannot find name 'ublic'.
tests/cases/compiler/strictModeReservedWord.ts(24,5): error TS1212: Identifier expected. 'static' is a reserved word in strict mode
tests/cases/compiler/strictModeReservedWord.ts(24,5): error TS2349: Cannot invoke an expression whose type lacks a call signature.
tests/cases/compiler/strictModeReservedWord.ts(24,5): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.


==== tests/cases/compiler/strictModeReservedWord.ts (43 errors) ====
Expand Down Expand Up @@ -153,7 +153,7 @@ tests/cases/compiler/strictModeReservedWord.ts(24,5): error TS2349: Cannot invok
~~~~~~
!!! error TS1212: Identifier expected. 'static' is a reserved word in strict mode
~~~~~~~~
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature.
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
}


Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/conformance/expressions/contextualTyping/superCallParameterContextualTyping2.ts(10,43): error TS2349: Cannot invoke an expression whose type lacks a call signature.
tests/cases/conformance/expressions/contextualTyping/superCallParameterContextualTyping2.ts(10,43): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'Number' has no compatible call signatures.


==== tests/cases/conformance/expressions/contextualTyping/superCallParameterContextualTyping2.ts (1 errors) ====
Expand All @@ -13,5 +13,5 @@ tests/cases/conformance/expressions/contextualTyping/superCallParameterContextua
// Ensure 'value' is not of type 'any' by invoking it with type arguments.
constructor() { super(value => String(value<string>())); }
~~~~~~~~~~~~~~~
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature.
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'Number' has no compatible call signatures.
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
tests/cases/conformance/es6/templates/templateStringInCallExpression.ts(1,1): error TS2349: Cannot invoke an expression whose type lacks a call signature.
tests/cases/conformance/es6/templates/templateStringInCallExpression.ts(1,1): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.


==== tests/cases/conformance/es6/templates/templateStringInCallExpression.ts (1 errors) ====
`abc${0}abc`(`hello ${0} world`, ` `, `1${2}3`);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature.
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
tests/cases/conformance/es6/templates/templateStringInCallExpressionES6.ts(1,1): error TS2349: Cannot invoke an expression whose type lacks a call signature.
tests/cases/conformance/es6/templates/templateStringInCallExpressionES6.ts(1,1): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.


==== tests/cases/conformance/es6/templates/templateStringInCallExpressionES6.ts (1 errors) ====
`abc${0}abc`(`hello ${0} world`, ` `, `1${2}3`);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature.
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
Loading