Skip to content

Commit 3885e3f

Browse files
authored
Fix error message regressed by microsoft#30916 (microsoft#31276)
1 parent fb6ae38 commit 3885e3f

6 files changed

+48
-4
lines changed

src/compiler/checker.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12290,7 +12290,7 @@ namespace ts {
1229012290
let depth = 0;
1229112291
let expandingFlags = ExpandingFlags.None;
1229212292
let overflow = false;
12293-
let suppressNextError = false;
12293+
let overrideNextErrorInfo: DiagnosticMessageChain | undefined;
1229412294

1229512295
Debug.assert(relation !== identityRelation || !errorNode, "no error reporting in identity checking");
1229612296

@@ -12571,10 +12571,14 @@ namespace ts {
1257112571
}
1257212572

1257312573
if (!result && reportErrors) {
12574-
const maybeSuppress = suppressNextError;
12575-
suppressNextError = false;
12574+
let maybeSuppress = overrideNextErrorInfo;
12575+
overrideNextErrorInfo = undefined;
1257612576
if (source.flags & TypeFlags.Object && target.flags & TypeFlags.Object) {
12577+
const currentError = errorInfo;
1257712578
tryElaborateArrayLikeErrors(source, target, reportErrors);
12579+
if (errorInfo !== currentError) {
12580+
maybeSuppress = errorInfo;
12581+
}
1257812582
}
1257912583
if (source.flags & TypeFlags.Object && target.flags & TypeFlags.Primitive) {
1258012584
tryElaborateErrorsForPrimitivesAndObjects(source, target);
@@ -13506,16 +13510,20 @@ namespace ts {
1350613510
if (unmatchedProperty) {
1350713511
if (reportErrors) {
1350813512
const props = arrayFrom(getUnmatchedProperties(source, target, requireOptionalProperties, /*matchDiscriminantProperties*/ false));
13513+
let shouldSkipElaboration = false;
1350913514
if (!headMessage || (headMessage.code !== Diagnostics.Class_0_incorrectly_implements_interface_1.code &&
1351013515
headMessage.code !== Diagnostics.Class_0_incorrectly_implements_class_1_Did_you_mean_to_extend_1_and_inherit_its_members_as_a_subclass.code)) {
13511-
suppressNextError = true; // Retain top-level error for interface implementing issues, otherwise omit it
13516+
shouldSkipElaboration = true; // Retain top-level error for interface implementing issues, otherwise omit it
1351213517
}
1351313518
if (props.length === 1) {
1351413519
const propName = symbolToString(unmatchedProperty);
1351513520
reportError(Diagnostics.Property_0_is_missing_in_type_1_but_required_in_type_2, propName, ...getTypeNamesForErrorDisplay(source, target));
1351613521
if (length(unmatchedProperty.declarations)) {
1351713522
associateRelatedInfo(createDiagnosticForNode(unmatchedProperty.declarations[0], Diagnostics._0_is_declared_here, propName));
1351813523
}
13524+
if (shouldSkipElaboration) {
13525+
overrideNextErrorInfo = errorInfo;
13526+
}
1351913527
}
1352013528
else if (tryElaborateArrayLikeErrors(source, target, /*reportErrors*/ false)) {
1352113529
if (props.length > 5) { // arbitrary cutoff for too-long list form
@@ -13524,7 +13532,11 @@ namespace ts {
1352413532
else {
1352513533
reportError(Diagnostics.Type_0_is_missing_the_following_properties_from_type_1_Colon_2, typeToString(source), typeToString(target), map(props, p => symbolToString(p)).join(", "));
1352613534
}
13535+
if (shouldSkipElaboration) {
13536+
overrideNextErrorInfo = errorInfo;
13537+
}
1352713538
}
13539+
// ELSE: No array like or unmatched property error - just issue top level error (errorInfo = undefined)
1352813540
}
1352913541
return Ternary.False;
1353013542
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
tests/cases/compiler/assigningFunctionToTupleIssuesError.ts(2,5): error TS2322: Type '() => void' is not assignable to type '[string]'.
2+
3+
4+
==== tests/cases/compiler/assigningFunctionToTupleIssuesError.ts (1 errors) ====
5+
declare let a: () => void;
6+
let b: [string] = a;
7+
~
8+
!!! error TS2322: Type '() => void' is not assignable to type '[string]'.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//// [assigningFunctionToTupleIssuesError.ts]
2+
declare let a: () => void;
3+
let b: [string] = a;
4+
5+
//// [assigningFunctionToTupleIssuesError.js]
6+
var b = a;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/assigningFunctionToTupleIssuesError.ts ===
2+
declare let a: () => void;
3+
>a : Symbol(a, Decl(assigningFunctionToTupleIssuesError.ts, 0, 11))
4+
5+
let b: [string] = a;
6+
>b : Symbol(b, Decl(assigningFunctionToTupleIssuesError.ts, 1, 3))
7+
>a : Symbol(a, Decl(assigningFunctionToTupleIssuesError.ts, 0, 11))
8+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/assigningFunctionToTupleIssuesError.ts ===
2+
declare let a: () => void;
3+
>a : () => void
4+
5+
let b: [string] = a;
6+
>b : [string]
7+
>a : () => void
8+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
declare let a: () => void;
2+
let b: [string] = a;

0 commit comments

Comments
 (0)