Skip to content

Prefer elaborating on expressions which could be called to produce a correct type by suggesting such #27016

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 2 commits into from
Sep 11, 2018
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
58 changes: 41 additions & 17 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10573,7 +10573,7 @@ namespace ts {

function checkTypeRelatedToAndOptionallyElaborate(source: Type, target: Type, relation: Map<RelationComparisonResult>, errorNode: Node | undefined, expr: Expression | undefined, headMessage?: DiagnosticMessage, containingMessageChain?: () => DiagnosticMessageChain | undefined): boolean {
if (isTypeRelatedTo(source, target, relation)) return true;
if (!errorNode || !elaborateError(expr, source, target)) {
if (!errorNode || !elaborateError(expr, source, target, relation)) {
return checkTypeRelatedTo(source, target, relation, errorNode, headMessage, containingMessageChain);
}
return false;
Expand All @@ -10583,25 +10583,49 @@ namespace ts {
return !!(type.flags & TypeFlags.Conditional || (type.flags & TypeFlags.Intersection && some((type as IntersectionType).types, isOrHasGenericConditional)));
}

function elaborateError(node: Expression | undefined, source: Type, target: Type): boolean {
function elaborateError(node: Expression | undefined, source: Type, target: Type, relation: Map<RelationComparisonResult>): boolean {
if (!node || isOrHasGenericConditional(target)) return false;
if (!checkTypeRelatedTo(source, target, relation, /*errorNode*/ undefined) && elaborateDidYouMeanToCallOrConstruct(node, source, target, relation)) {
return true;
}
switch (node.kind) {
case SyntaxKind.JsxExpression:
case SyntaxKind.ParenthesizedExpression:
return elaborateError((node as ParenthesizedExpression | JsxExpression).expression, source, target);
return elaborateError((node as ParenthesizedExpression | JsxExpression).expression, source, target, relation);
case SyntaxKind.BinaryExpression:
switch ((node as BinaryExpression).operatorToken.kind) {
case SyntaxKind.EqualsToken:
case SyntaxKind.CommaToken:
return elaborateError((node as BinaryExpression).right, source, target);
return elaborateError((node as BinaryExpression).right, source, target, relation);
}
break;
case SyntaxKind.ObjectLiteralExpression:
return elaborateObjectLiteral(node as ObjectLiteralExpression, source, target);
return elaborateObjectLiteral(node as ObjectLiteralExpression, source, target, relation);
case SyntaxKind.ArrayLiteralExpression:
return elaborateArrayLiteral(node as ArrayLiteralExpression, source, target);
return elaborateArrayLiteral(node as ArrayLiteralExpression, source, target, relation);
case SyntaxKind.JsxAttributes:
return elaborateJsxAttributes(node as JsxAttributes, source, target);
return elaborateJsxAttributes(node as JsxAttributes, source, target, relation);
}
return false;
}

function elaborateDidYouMeanToCallOrConstruct(node: Expression, source: Type, target: Type, relation: Map<RelationComparisonResult>): boolean {
const callSignatures = getSignaturesOfType(source, SignatureKind.Call);
const constructSignatures = getSignaturesOfType(source, SignatureKind.Construct);
for (const signatures of [constructSignatures, callSignatures]) {
if (some(signatures, s => {
const returnType = getReturnTypeOfSignature(s);
return !(returnType.flags & (TypeFlags.Any | TypeFlags.Never)) && checkTypeRelatedTo(returnType, target, relation, /*errorNode*/ undefined);
})) {
const resultObj: { error?: Diagnostic } = {};
checkTypeAssignableTo(source, target, node, /*errorMessage*/ undefined, /*containingChain*/ undefined, resultObj);
const diagnostic = resultObj.error!;
addRelatedInfo(diagnostic, createDiagnosticForNode(
node,
signatures === constructSignatures ? Diagnostics.Did_you_mean_to_use_new_with_this_expression : Diagnostics.Did_you_mean_to_call_this_expression
));
return true;
}
}
return false;
}
Expand All @@ -10612,15 +10636,15 @@ namespace ts {
* If that element would issue an error, we first attempt to dive into that element's inner expression and issue a more specific error by recuring into `elaborateError`
* Otherwise, we issue an error on _every_ element which fail the assignability check
*/
function elaborateElementwise(iterator: ElaborationIterator, source: Type, target: Type) {
function elaborateElementwise(iterator: ElaborationIterator, source: Type, target: Type, relation: Map<RelationComparisonResult>) {
// Assignability failure - check each prop individually, and if that fails, fall back on the bad error span
let reportedError = false;
for (let status = iterator.next(); !status.done; status = iterator.next()) {
const { errorNode: prop, innerExpression: next, nameType, errorMessage } = status.value;
const sourcePropType = getIndexedAccessType(source, nameType, /*accessNode*/ undefined, errorType);
const targetPropType = getIndexedAccessType(target, nameType, /*accessNode*/ undefined, errorType);
if (sourcePropType !== errorType && targetPropType !== errorType && !isTypeAssignableTo(sourcePropType, targetPropType)) {
const elaborated = next && elaborateError(next, sourcePropType, targetPropType);
const elaborated = next && elaborateError(next, sourcePropType, targetPropType, relation);
if (elaborated) {
reportedError = true;
}
Expand All @@ -10629,10 +10653,10 @@ namespace ts {
const resultObj: { error?: Diagnostic } = {};
// Use the expression type, if available
const specificSource = next ? checkExpressionForMutableLocation(next, CheckMode.Normal, sourcePropType) : sourcePropType;
const result = checkTypeAssignableTo(specificSource, targetPropType, prop, errorMessage, /*containingChain*/ undefined, resultObj);
const result = checkTypeRelatedTo(specificSource, targetPropType, relation, prop, errorMessage, /*containingChain*/ undefined, resultObj);
if (result && specificSource !== sourcePropType) {
// If for whatever reason the expression type doesn't yield an error, make sure we still issue an error on the sourcePropType
checkTypeAssignableTo(sourcePropType, targetPropType, prop, errorMessage, /*containingChain*/ undefined, resultObj);
checkTypeRelatedTo(sourcePropType, targetPropType, relation, prop, errorMessage, /*containingChain*/ undefined, resultObj);
}
if (resultObj.error) {
const reportedDiag = resultObj.error;
Expand Down Expand Up @@ -10674,8 +10698,8 @@ namespace ts {
}
}

function elaborateJsxAttributes(node: JsxAttributes, source: Type, target: Type) {
return elaborateElementwise(generateJsxAttributes(node), source, target);
function elaborateJsxAttributes(node: JsxAttributes, source: Type, target: Type, relation: Map<RelationComparisonResult>) {
return elaborateElementwise(generateJsxAttributes(node), source, target, relation);
}

function *generateLimitedTupleElements(node: ArrayLiteralExpression, target: Type): ElaborationIterator {
Expand All @@ -10691,9 +10715,9 @@ namespace ts {
}
}

function elaborateArrayLiteral(node: ArrayLiteralExpression, source: Type, target: Type) {
function elaborateArrayLiteral(node: ArrayLiteralExpression, source: Type, target: Type, relation: Map<RelationComparisonResult>) {
if (isTupleLikeType(source)) {
return elaborateElementwise(generateLimitedTupleElements(node, target), source, target);
return elaborateElementwise(generateLimitedTupleElements(node, target), source, target, relation);
}
return false;
}
Expand Down Expand Up @@ -10722,8 +10746,8 @@ namespace ts {
}
}

function elaborateObjectLiteral(node: ObjectLiteralExpression, source: Type, target: Type) {
return elaborateElementwise(generateObjectLiteralElements(node), source, target);
function elaborateObjectLiteral(node: ObjectLiteralExpression, source: Type, target: Type, relation: Map<RelationComparisonResult>) {
return elaborateElementwise(generateObjectLiteralElements(node), source, target, relation);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3716,6 +3716,14 @@
"category": "Message",
"code": 6211
},
"Did you mean to call this expression?": {
"category": "Message",
"code": 6212
},
"Did you mean to use `new` with this expression?": {
Copy link
Member

Choose a reason for hiding this comment

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

new [](start = 25, length = 5)

Elsewhere, "new" is in single quotes.

"category": "Message",
"code": 6213
},

"Projects to reference": {
"category": "Message",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(10,8): error TS2322: Type 'typeof Bar' is not assignable to type 'Bar'.
Property 'x' is missing in type 'typeof Bar'.
tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(11,8): error TS2322: Type 'DateConstructor' is not assignable to type 'Date'.
Property 'toDateString' is missing in type 'DateConstructor'.
tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(17,4): error TS2322: Type '() => number' is not assignable to type 'number'.
tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(26,5): error TS2322: Type '() => number' is not assignable to type 'number'.


==== tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts (4 errors) ====
class Bar {
x!: string;
}

declare function getNum(): number;

declare function foo(arg: { x: Bar, y: Date }, item: number, items?: [number, number, number]): void;

foo({
x: Bar,
~~~
!!! error TS2322: Type 'typeof Bar' is not assignable to type 'Bar'.
!!! error TS2322: Property 'x' is missing in type 'typeof Bar'.
!!! related TS6213 tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts:10:8: Did you mean to use `new` with this expression?
y: Date
~~~~
!!! error TS2322: Type 'DateConstructor' is not assignable to type 'Date'.
!!! error TS2322: Property 'toDateString' is missing in type 'DateConstructor'.
!!! related TS6213 tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts:11:8: Did you mean to use `new` with this expression?
}, getNum());

foo({
x: new Bar(),
y: new Date()
}, getNum);
~~~~~~
!!! error TS2322: Type '() => number' is not assignable to type 'number'.
!!! related TS6212 tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts:17:4: Did you mean to call this expression?


foo({
x: new Bar(),
y: new Date()
}, getNum(), [
1,
2,
getNum
~~~~~~
!!! error TS2322: Type '() => number' is not assignable to type 'number'.
!!! related TS6212 tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts:26:5: Did you mean to call this expression?
]);

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//// [didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts]
class Bar {
x!: string;
}

declare function getNum(): number;

declare function foo(arg: { x: Bar, y: Date }, item: number, items?: [number, number, number]): void;

foo({
x: Bar,
y: Date
}, getNum());

foo({
x: new Bar(),
y: new Date()
}, getNum);


foo({
x: new Bar(),
y: new Date()
}, getNum(), [
1,
2,
getNum
]);


//// [didYouMeanElaborationsForExpressionsWhichCouldBeCalled.js]
var Bar = /** @class */ (function () {
function Bar() {
}
return Bar;
}());
foo({
x: Bar,
y: Date
}, getNum());
foo({
x: new Bar(),
y: new Date()
}, getNum);
foo({
x: new Bar(),
y: new Date()
}, getNum(), [
1,
2,
getNum
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
=== tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts ===
class Bar {
>Bar : Symbol(Bar, Decl(didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts, 0, 0))

x!: string;
>x : Symbol(Bar.x, Decl(didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts, 0, 11))
}

declare function getNum(): number;
>getNum : Symbol(getNum, Decl(didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts, 2, 1))

declare function foo(arg: { x: Bar, y: Date }, item: number, items?: [number, number, number]): void;
>foo : Symbol(foo, Decl(didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts, 4, 34))
>arg : Symbol(arg, Decl(didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts, 6, 21))
>x : Symbol(x, Decl(didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts, 6, 27))
>Bar : Symbol(Bar, Decl(didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts, 0, 0))
>y : Symbol(y, Decl(didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts, 6, 35))
>Date : Symbol(Date, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.scripthost.d.ts, --, --))
>item : Symbol(item, Decl(didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts, 6, 46))
>items : Symbol(items, Decl(didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts, 6, 60))

foo({
>foo : Symbol(foo, Decl(didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts, 4, 34))

x: Bar,
>x : Symbol(x, Decl(didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts, 8, 5))
>Bar : Symbol(Bar, Decl(didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts, 0, 0))

y: Date
>y : Symbol(y, Decl(didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts, 9, 11))
>Date : Symbol(Date, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.scripthost.d.ts, --, --))

}, getNum());
>getNum : Symbol(getNum, Decl(didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts, 2, 1))

foo({
>foo : Symbol(foo, Decl(didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts, 4, 34))

x: new Bar(),
>x : Symbol(x, Decl(didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts, 13, 5))
>Bar : Symbol(Bar, Decl(didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts, 0, 0))

y: new Date()
>y : Symbol(y, Decl(didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts, 14, 17))
>Date : Symbol(Date, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.scripthost.d.ts, --, --))

}, getNum);
>getNum : Symbol(getNum, Decl(didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts, 2, 1))


foo({
>foo : Symbol(foo, Decl(didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts, 4, 34))

x: new Bar(),
>x : Symbol(x, Decl(didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts, 19, 5))
>Bar : Symbol(Bar, Decl(didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts, 0, 0))

y: new Date()
>y : Symbol(y, Decl(didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts, 20, 17))
>Date : Symbol(Date, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.scripthost.d.ts, --, --))

}, getNum(), [
>getNum : Symbol(getNum, Decl(didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts, 2, 1))

1,
2,
getNum
>getNum : Symbol(getNum, Decl(didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts, 2, 1))

]);

Loading