Skip to content

Commit 921863e

Browse files
authored
Use head message at top level of elaboration if elaborating via possible call (#27107)
* Use head message at top level of elaboration if elaborating via possible call * Accept updated baseline
1 parent e471856 commit 921863e

8 files changed

+61
-16
lines changed

src/compiler/checker.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -10592,7 +10592,7 @@ namespace ts {
1059210592

1059310593
function checkTypeRelatedToAndOptionallyElaborate(source: Type, target: Type, relation: Map<RelationComparisonResult>, errorNode: Node | undefined, expr: Expression | undefined, headMessage?: DiagnosticMessage, containingMessageChain?: () => DiagnosticMessageChain | undefined): boolean {
1059410594
if (isTypeRelatedTo(source, target, relation)) return true;
10595-
if (!errorNode || !elaborateError(expr, source, target, relation)) {
10595+
if (!errorNode || !elaborateError(expr, source, target, relation, headMessage)) {
1059610596
return checkTypeRelatedTo(source, target, relation, errorNode, headMessage, containingMessageChain);
1059710597
}
1059810598
return false;
@@ -10602,20 +10602,20 @@ namespace ts {
1060210602
return !!(type.flags & TypeFlags.Conditional || (type.flags & TypeFlags.Intersection && some((type as IntersectionType).types, isOrHasGenericConditional)));
1060310603
}
1060410604

10605-
function elaborateError(node: Expression | undefined, source: Type, target: Type, relation: Map<RelationComparisonResult>): boolean {
10605+
function elaborateError(node: Expression | undefined, source: Type, target: Type, relation: Map<RelationComparisonResult>, headMessage: DiagnosticMessage | undefined): boolean {
1060610606
if (!node || isOrHasGenericConditional(target)) return false;
10607-
if (!checkTypeRelatedTo(source, target, relation, /*errorNode*/ undefined) && elaborateDidYouMeanToCallOrConstruct(node, source, target, relation)) {
10607+
if (!checkTypeRelatedTo(source, target, relation, /*errorNode*/ undefined) && elaborateDidYouMeanToCallOrConstruct(node, source, target, relation, headMessage)) {
1060810608
return true;
1060910609
}
1061010610
switch (node.kind) {
1061110611
case SyntaxKind.JsxExpression:
1061210612
case SyntaxKind.ParenthesizedExpression:
10613-
return elaborateError((node as ParenthesizedExpression | JsxExpression).expression, source, target, relation);
10613+
return elaborateError((node as ParenthesizedExpression | JsxExpression).expression, source, target, relation, headMessage);
1061410614
case SyntaxKind.BinaryExpression:
1061510615
switch ((node as BinaryExpression).operatorToken.kind) {
1061610616
case SyntaxKind.EqualsToken:
1061710617
case SyntaxKind.CommaToken:
10618-
return elaborateError((node as BinaryExpression).right, source, target, relation);
10618+
return elaborateError((node as BinaryExpression).right, source, target, relation, headMessage);
1061910619
}
1062010620
break;
1062110621
case SyntaxKind.ObjectLiteralExpression:
@@ -10628,7 +10628,7 @@ namespace ts {
1062810628
return false;
1062910629
}
1063010630

10631-
function elaborateDidYouMeanToCallOrConstruct(node: Expression, source: Type, target: Type, relation: Map<RelationComparisonResult>): boolean {
10631+
function elaborateDidYouMeanToCallOrConstruct(node: Expression, source: Type, target: Type, relation: Map<RelationComparisonResult>, headMessage: DiagnosticMessage | undefined): boolean {
1063210632
const callSignatures = getSignaturesOfType(source, SignatureKind.Call);
1063310633
const constructSignatures = getSignaturesOfType(source, SignatureKind.Construct);
1063410634
for (const signatures of [constructSignatures, callSignatures]) {
@@ -10637,7 +10637,7 @@ namespace ts {
1063710637
return !(returnType.flags & (TypeFlags.Any | TypeFlags.Never)) && checkTypeRelatedTo(returnType, target, relation, /*errorNode*/ undefined);
1063810638
})) {
1063910639
const resultObj: { error?: Diagnostic } = {};
10640-
checkTypeAssignableTo(source, target, node, /*errorMessage*/ undefined, /*containingChain*/ undefined, resultObj);
10640+
checkTypeAssignableTo(source, target, node, headMessage, /*containingChain*/ undefined, resultObj);
1064110641
const diagnostic = resultObj.error!;
1064210642
addRelatedInfo(diagnostic, createDiagnosticForNode(
1064310643
node,
@@ -10663,7 +10663,7 @@ namespace ts {
1066310663
const sourcePropType = getIndexedAccessType(source, nameType, /*accessNode*/ undefined, errorType);
1066410664
const targetPropType = getIndexedAccessType(target, nameType, /*accessNode*/ undefined, errorType);
1066510665
if (sourcePropType !== errorType && targetPropType !== errorType && !isTypeAssignableTo(sourcePropType, targetPropType)) {
10666-
const elaborated = next && elaborateError(next, sourcePropType, targetPropType, relation);
10666+
const elaborated = next && elaborateError(next, sourcePropType, targetPropType, relation, /*headMessage*/ undefined);
1066710667
if (elaborated) {
1066810668
reportedError = true;
1066910669
}

tests/baselines/reference/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(1
22
Property 'x' is missing in type 'typeof Bar'.
33
tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(11,8): error TS2322: Type 'DateConstructor' is not assignable to type 'Date'.
44
Property 'toDateString' is missing in type 'DateConstructor'.
5-
tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(17,4): error TS2322: Type '() => number' is not assignable to type 'number'.
5+
tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(17,4): error TS2345: Argument of type '() => number' is not assignable to parameter of type 'number'.
66
tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(26,5): error TS2322: Type '() => number' is not assignable to type 'number'.
77

88

@@ -33,7 +33,7 @@ tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(2
3333
y: new Date()
3434
}, getNum);
3535
~~~~~~
36-
!!! error TS2322: Type '() => number' is not assignable to type 'number'.
36+
!!! error TS2345: Argument of type '() => number' is not assignable to parameter of type 'number'.
3737
!!! related TS6212 tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts:17:4: Did you mean to call this expression?
3838

3939

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts(3,4): error TS2345: Argument of type 'new () => number' is not assignable to parameter of type 'number'.
2+
3+
4+
==== tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts (1 errors) ====
5+
declare var ohno: new () => number;
6+
declare function ff(t: number): void;
7+
ff(ohno)
8+
~~~~
9+
!!! error TS2345: Argument of type 'new () => number' is not assignable to parameter of type 'number'.
10+
!!! related TS6213 tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts:3:4: Did you mean to use 'new' with this expression?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts]
2+
declare var ohno: new () => number;
3+
declare function ff(t: number): void;
4+
ff(ohno)
5+
6+
//// [elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.js]
7+
ff(ohno);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts ===
2+
declare var ohno: new () => number;
3+
>ohno : Symbol(ohno, Decl(elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts, 0, 11))
4+
5+
declare function ff(t: number): void;
6+
>ff : Symbol(ff, Decl(elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts, 0, 35))
7+
>t : Symbol(t, Decl(elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts, 1, 20))
8+
9+
ff(ohno)
10+
>ff : Symbol(ff, Decl(elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts, 0, 35))
11+
>ohno : Symbol(ohno, Decl(elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts, 0, 11))
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts ===
2+
declare var ohno: new () => number;
3+
>ohno : new () => number
4+
5+
declare function ff(t: number): void;
6+
>ff : (t: number) => void
7+
>t : number
8+
9+
ff(ohno)
10+
>ff(ohno) : void
11+
>ff : (t: number) => void
12+
>ohno : new () => number
13+
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts(7,5): error TS2322: Type '() => (x: string) => string' is not assignable to type '(x: string) => string'.
1+
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'.
22
Type '(x: string) => string' is not assignable to type 'string'.
3-
tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts(8,5): error TS2322: Type '() => (x: string) => string' is not assignable to type '(x: string) => string'.
3+
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'.
44
Type '(x: string) => string' is not assignable to type 'string'.
55

66

@@ -13,12 +13,12 @@ tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts(8,5):
1313
foo(g);
1414
foo(() => g);
1515
~~~~~~~
16-
!!! error TS2322: Type '() => (x: string) => string' is not assignable to type '(x: string) => string'.
17-
!!! error TS2322: Type '(x: string) => string' is not assignable to type 'string'.
16+
!!! error TS2345: Argument of type '() => (x: string) => string' is not assignable to parameter of type '(x: string) => string'.
17+
!!! error TS2345: Type '(x: string) => string' is not assignable to type 'string'.
1818
!!! related TS6212 tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts:7:5: Did you mean to call this expression?
1919
foo(x);
2020
~
21-
!!! error TS2322: Type '() => (x: string) => string' is not assignable to type '(x: string) => string'.
22-
!!! error TS2322: Type '(x: string) => string' is not assignable to type 'string'.
21+
!!! error TS2345: Argument of type '() => (x: string) => string' is not assignable to parameter of type '(x: string) => string'.
22+
!!! error TS2345: Type '(x: string) => string' is not assignable to type 'string'.
2323
!!! related TS6212 tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts:8:5: Did you mean to call this expression?
2424

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare var ohno: new () => number;
2+
declare function ff(t: number): void;
3+
ff(ohno)

0 commit comments

Comments
 (0)