diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 4e31e285e2de3..2a8b0ce43147e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10592,7 +10592,7 @@ namespace ts { function checkTypeRelatedToAndOptionallyElaborate(source: Type, target: Type, relation: Map, 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, relation)) { + if (!errorNode || !elaborateError(expr, source, target, relation, headMessage)) { return checkTypeRelatedTo(source, target, relation, errorNode, headMessage, containingMessageChain); } return false; @@ -10602,20 +10602,20 @@ 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, relation: Map): boolean { + function elaborateError(node: Expression | undefined, source: Type, target: Type, relation: Map, headMessage: DiagnosticMessage | undefined): boolean { if (!node || isOrHasGenericConditional(target)) return false; - if (!checkTypeRelatedTo(source, target, relation, /*errorNode*/ undefined) && elaborateDidYouMeanToCallOrConstruct(node, source, target, relation)) { + if (!checkTypeRelatedTo(source, target, relation, /*errorNode*/ undefined) && elaborateDidYouMeanToCallOrConstruct(node, source, target, relation, headMessage)) { return true; } switch (node.kind) { case SyntaxKind.JsxExpression: case SyntaxKind.ParenthesizedExpression: - return elaborateError((node as ParenthesizedExpression | JsxExpression).expression, source, target, relation); + return elaborateError((node as ParenthesizedExpression | JsxExpression).expression, source, target, relation, headMessage); case SyntaxKind.BinaryExpression: switch ((node as BinaryExpression).operatorToken.kind) { case SyntaxKind.EqualsToken: case SyntaxKind.CommaToken: - return elaborateError((node as BinaryExpression).right, source, target, relation); + return elaborateError((node as BinaryExpression).right, source, target, relation, headMessage); } break; case SyntaxKind.ObjectLiteralExpression: @@ -10628,7 +10628,7 @@ namespace ts { return false; } - function elaborateDidYouMeanToCallOrConstruct(node: Expression, source: Type, target: Type, relation: Map): boolean { + function elaborateDidYouMeanToCallOrConstruct(node: Expression, source: Type, target: Type, relation: Map, headMessage: DiagnosticMessage | undefined): boolean { const callSignatures = getSignaturesOfType(source, SignatureKind.Call); const constructSignatures = getSignaturesOfType(source, SignatureKind.Construct); for (const signatures of [constructSignatures, callSignatures]) { @@ -10637,7 +10637,7 @@ namespace ts { 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); + checkTypeAssignableTo(source, target, node, headMessage, /*containingChain*/ undefined, resultObj); const diagnostic = resultObj.error!; addRelatedInfo(diagnostic, createDiagnosticForNode( node, @@ -10663,7 +10663,7 @@ namespace ts { 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, relation); + const elaborated = next && elaborateError(next, sourcePropType, targetPropType, relation, /*headMessage*/ undefined); if (elaborated) { reportedError = true; } diff --git a/tests/baselines/reference/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.errors.txt b/tests/baselines/reference/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.errors.txt index 17f5cf6a665d5..4af77e234f995 100644 --- a/tests/baselines/reference/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.errors.txt +++ b/tests/baselines/reference/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.errors.txt @@ -2,7 +2,7 @@ tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(1 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(17,4): error TS2345: Argument of type '() => number' is not assignable to parameter of type 'number'. tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(26,5): error TS2322: Type '() => number' is not assignable to type 'number'. @@ -33,7 +33,7 @@ tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(2 y: new Date() }, getNum); ~~~~~~ -!!! error TS2322: Type '() => number' is not assignable to type 'number'. +!!! error TS2345: Argument of type '() => number' is not assignable to parameter of type 'number'. !!! related TS6212 tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts:17:4: Did you mean to call this expression? diff --git a/tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.errors.txt b/tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.errors.txt new file mode 100644 index 0000000000000..b8400f3b76b70 --- /dev/null +++ b/tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.errors.txt @@ -0,0 +1,10 @@ +tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts(3,4): error TS2345: Argument of type 'new () => number' is not assignable to parameter of type 'number'. + + +==== tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts (1 errors) ==== + declare var ohno: new () => number; + declare function ff(t: number): void; + ff(ohno) + ~~~~ +!!! error TS2345: Argument of type 'new () => number' is not assignable to parameter of type 'number'. +!!! related TS6213 tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts:3:4: Did you mean to use 'new' with this expression? \ No newline at end of file diff --git a/tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.js b/tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.js new file mode 100644 index 0000000000000..bf7e6a98bbd1a --- /dev/null +++ b/tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.js @@ -0,0 +1,7 @@ +//// [elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts] +declare var ohno: new () => number; +declare function ff(t: number): void; +ff(ohno) + +//// [elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.js] +ff(ohno); diff --git a/tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.symbols b/tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.symbols new file mode 100644 index 0000000000000..8913bb59c9e2f --- /dev/null +++ b/tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.symbols @@ -0,0 +1,12 @@ +=== tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts === +declare var ohno: new () => number; +>ohno : Symbol(ohno, Decl(elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts, 0, 11)) + +declare function ff(t: number): void; +>ff : Symbol(ff, Decl(elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts, 0, 35)) +>t : Symbol(t, Decl(elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts, 1, 20)) + +ff(ohno) +>ff : Symbol(ff, Decl(elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts, 0, 35)) +>ohno : Symbol(ohno, Decl(elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts, 0, 11)) + diff --git a/tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.types b/tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.types new file mode 100644 index 0000000000000..cb47b67754ff6 --- /dev/null +++ b/tests/baselines/reference/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.types @@ -0,0 +1,13 @@ +=== tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts === +declare var ohno: new () => number; +>ohno : new () => number + +declare function ff(t: number): void; +>ff : (t: number) => void +>t : number + +ff(ohno) +>ff(ohno) : void +>ff : (t: number) => void +>ohno : new () => number + diff --git a/tests/baselines/reference/parser536727.errors.txt b/tests/baselines/reference/parser536727.errors.txt index 6cdf152582f38..8c2cb059a78a7 100644 --- a/tests/baselines/reference/parser536727.errors.txt +++ b/tests/baselines/reference/parser536727.errors.txt @@ -1,6 +1,6 @@ -tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts(7,5): error TS2322: Type '() => (x: string) => string' is not assignable to type '(x: string) => string'. +tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts(7,5): error TS2345: Argument of type '() => (x: string) => string' is not assignable to parameter of type '(x: string) => string'. Type '(x: string) => string' is not assignable to type 'string'. -tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts(8,5): error TS2322: Type '() => (x: string) => string' is not assignable to type '(x: string) => string'. +tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts(8,5): error TS2345: Argument of type '() => (x: string) => string' is not assignable to parameter of type '(x: string) => string'. Type '(x: string) => string' is not assignable to type 'string'. @@ -13,12 +13,12 @@ tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts(8,5): foo(g); foo(() => g); ~~~~~~~ -!!! error TS2322: Type '() => (x: string) => string' is not assignable to type '(x: string) => string'. -!!! error TS2322: Type '(x: string) => string' is not assignable to type 'string'. +!!! error TS2345: Argument of type '() => (x: string) => string' is not assignable to parameter of type '(x: string) => string'. +!!! error TS2345: Type '(x: string) => string' is not assignable to type 'string'. !!! related TS6212 tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts:7:5: Did you mean to call this expression? foo(x); ~ -!!! error TS2322: Type '() => (x: string) => string' is not assignable to type '(x: string) => string'. -!!! error TS2322: Type '(x: string) => string' is not assignable to type 'string'. +!!! error TS2345: Argument of type '() => (x: string) => string' is not assignable to parameter of type '(x: string) => string'. +!!! error TS2345: Type '(x: string) => string' is not assignable to type 'string'. !!! related TS6212 tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts:8:5: Did you mean to call this expression? \ No newline at end of file diff --git a/tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts b/tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts new file mode 100644 index 0000000000000..f2b964fadbc51 --- /dev/null +++ b/tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts @@ -0,0 +1,3 @@ +declare var ohno: new () => number; +declare function ff(t: number): void; +ff(ohno) \ No newline at end of file