Skip to content

Commit 9a771d5

Browse files
Skip parent error when reporting excess property checks (#55152)
1 parent 5ea2952 commit 9a771d5

File tree

117 files changed

+855
-1236
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+855
-1236
lines changed

src/compiler/checker.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20369,6 +20369,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2036920369
let expandingFlags = ExpandingFlags.None;
2037020370
let overflow = false;
2037120371
let overrideNextErrorInfo = 0; // How many `reportRelationError` calls should be skipped in the elaboration pyramid
20372+
let skipParentCounter = 0; // How many errors should be skipped 'above' in the elaboration pyramid
2037220373
let lastSkippedInfo: [Type, Type] | undefined;
2037320374
let incompatibleStack: DiagnosticAndArguments[] | undefined;
2037420375

@@ -20430,6 +20431,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2043020431
lastSkippedInfo = saved.lastSkippedInfo;
2043120432
incompatibleStack = saved.incompatibleStack;
2043220433
overrideNextErrorInfo = saved.overrideNextErrorInfo;
20434+
skipParentCounter = saved.skipParentCounter;
2043320435
relatedInfo = saved.relatedInfo;
2043420436
}
2043520437

@@ -20439,6 +20441,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2043920441
lastSkippedInfo,
2044020442
incompatibleStack: incompatibleStack?.slice(),
2044120443
overrideNextErrorInfo,
20444+
skipParentCounter,
2044220445
relatedInfo: relatedInfo?.slice() as [DiagnosticRelatedInformation, ...DiagnosticRelatedInformation[]] | undefined,
2044320446
};
2044420447
}
@@ -20561,7 +20564,17 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2056120564
Debug.assert(!!errorNode);
2056220565
if (incompatibleStack) reportIncompatibleStack();
2056320566
if (message.elidedInCompatabilityPyramid) return;
20564-
errorInfo = chainDiagnosticMessages(errorInfo, message, ...args);
20567+
if (skipParentCounter === 0) {
20568+
errorInfo = chainDiagnosticMessages(errorInfo, message, ...args);
20569+
}
20570+
else {
20571+
skipParentCounter--;
20572+
}
20573+
}
20574+
20575+
function reportParentSkippedError(message: DiagnosticMessage, ...args: DiagnosticArguments): void {
20576+
reportError(message, ...args);
20577+
skipParentCounter++;
2056520578
}
2056620579

2056720580
function associateRelatedInfo(info: DiagnosticRelatedInformation) {
@@ -20958,11 +20971,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2095820971
}
2095920972
}
2096020973
if (suggestion !== undefined) {
20961-
reportError(Diagnostics.Object_literal_may_only_specify_known_properties_but_0_does_not_exist_in_type_1_Did_you_mean_to_write_2,
20974+
reportParentSkippedError(Diagnostics.Object_literal_may_only_specify_known_properties_but_0_does_not_exist_in_type_1_Did_you_mean_to_write_2,
2096220975
symbolToString(prop), typeToString(errorTarget), suggestion);
2096320976
}
2096420977
else {
20965-
reportError(Diagnostics.Object_literal_may_only_specify_known_properties_and_0_does_not_exist_in_type_1,
20978+
reportParentSkippedError(Diagnostics.Object_literal_may_only_specify_known_properties_and_0_does_not_exist_in_type_1,
2096620979
symbolToString(prop), typeToString(errorTarget));
2096720980
}
2096820981
}
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
arrayCast.ts(3,23): error TS2352: Conversion of type '{ foo: string; }[]' to type '{ id: number; }[]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
2-
Type '{ foo: string; }' is not comparable to type '{ id: number; }'.
3-
Object literal may only specify known properties, and 'foo' does not exist in type '{ id: number; }'.
2+
Object literal may only specify known properties, and 'foo' does not exist in type '{ id: number; }'.
43

54

65
==== arrayCast.ts (1 errors) ====
@@ -9,8 +8,7 @@ arrayCast.ts(3,23): error TS2352: Conversion of type '{ foo: string; }[]' to typ
98
<{ id: number; }[]>[{ foo: "s" }];
109
~~~
1110
!!! error TS2352: Conversion of type '{ foo: string; }[]' to type '{ id: number; }[]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
12-
!!! error TS2352: Type '{ foo: string; }' is not comparable to type '{ id: number; }'.
13-
!!! error TS2352: Object literal may only specify known properties, and 'foo' does not exist in type '{ id: number; }'.
11+
!!! error TS2352: Object literal may only specify known properties, and 'foo' does not exist in type '{ id: number; }'.
1412

1513
// Should succeed, as the {} element causes the type of the array to be {}[]
1614
<{ id: number; }[]>[{ foo: "s" }, {}];

tests/baselines/reference/arrayLiteralTypeInference.errors.txt

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
arrayLiteralTypeInference.ts(14,14): error TS2322: Type '{ id: number; trueness: false; }' is not assignable to type 'Action'.
2-
Object literal may only specify known properties, and 'trueness' does not exist in type 'Action'.
3-
arrayLiteralTypeInference.ts(15,14): error TS2322: Type '{ id: number; name: string; }' is not assignable to type 'Action'.
4-
Object literal may only specify known properties, and 'name' does not exist in type 'Action'.
5-
arrayLiteralTypeInference.ts(31,18): error TS2322: Type '{ id: number; trueness: false; }' is not assignable to type '{ id: number; }'.
6-
Object literal may only specify known properties, and 'trueness' does not exist in type '{ id: number; }'.
7-
arrayLiteralTypeInference.ts(32,18): error TS2322: Type '{ id: number; name: string; }' is not assignable to type '{ id: number; }'.
8-
Object literal may only specify known properties, and 'name' does not exist in type '{ id: number; }'.
1+
arrayLiteralTypeInference.ts(14,14): error TS2353: Object literal may only specify known properties, and 'trueness' does not exist in type 'Action'.
2+
arrayLiteralTypeInference.ts(15,14): error TS2353: Object literal may only specify known properties, and 'name' does not exist in type 'Action'.
3+
arrayLiteralTypeInference.ts(31,18): error TS2353: Object literal may only specify known properties, and 'trueness' does not exist in type '{ id: number; }'.
4+
arrayLiteralTypeInference.ts(32,18): error TS2353: Object literal may only specify known properties, and 'name' does not exist in type '{ id: number; }'.
95

106

117
==== arrayLiteralTypeInference.ts (4 errors) ====
@@ -24,12 +20,10 @@ arrayLiteralTypeInference.ts(32,18): error TS2322: Type '{ id: number; name: str
2420
var x1: Action[] = [
2521
{ id: 2, trueness: false },
2622
~~~~~~~~
27-
!!! error TS2322: Type '{ id: number; trueness: false; }' is not assignable to type 'Action'.
28-
!!! error TS2322: Object literal may only specify known properties, and 'trueness' does not exist in type 'Action'.
23+
!!! error TS2353: Object literal may only specify known properties, and 'trueness' does not exist in type 'Action'.
2924
{ id: 3, name: "three" }
3025
~~~~
31-
!!! error TS2322: Type '{ id: number; name: string; }' is not assignable to type 'Action'.
32-
!!! error TS2322: Object literal may only specify known properties, and 'name' does not exist in type 'Action'.
26+
!!! error TS2353: Object literal may only specify known properties, and 'name' does not exist in type 'Action'.
3327
]
3428

3529
var x2: Action[] = [
@@ -47,12 +41,10 @@ arrayLiteralTypeInference.ts(32,18): error TS2322: Type '{ id: number; name: str
4741
[
4842
{ id: 2, trueness: false },
4943
~~~~~~~~
50-
!!! error TS2322: Type '{ id: number; trueness: false; }' is not assignable to type '{ id: number; }'.
51-
!!! error TS2322: Object literal may only specify known properties, and 'trueness' does not exist in type '{ id: number; }'.
44+
!!! error TS2353: Object literal may only specify known properties, and 'trueness' does not exist in type '{ id: number; }'.
5245
{ id: 3, name: "three" }
5346
~~~~
54-
!!! error TS2322: Type '{ id: number; name: string; }' is not assignable to type '{ id: number; }'.
55-
!!! error TS2322: Object literal may only specify known properties, and 'name' does not exist in type '{ id: number; }'.
47+
!!! error TS2353: Object literal may only specify known properties, and 'name' does not exist in type '{ id: number; }'.
5648
]
5749

5850
var z2: { id: number }[] =

tests/baselines/reference/arrayLiterals.errors.txt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
arrayLiterals.ts(24,77): error TS2322: Type '{ a: string; b: number; c: string; }' is not assignable to type '{ a: string; b: number; }'.
2-
Object literal may only specify known properties, and 'c' does not exist in type '{ a: string; b: number; }'.
3-
arrayLiterals.ts(24,101): error TS2322: Type '{ a: string; b: number; c: number; }' is not assignable to type '{ a: string; b: number; }'.
4-
Object literal may only specify known properties, and 'c' does not exist in type '{ a: string; b: number; }'.
1+
arrayLiterals.ts(24,77): error TS2353: Object literal may only specify known properties, and 'c' does not exist in type '{ a: string; b: number; }'.
2+
arrayLiterals.ts(24,101): error TS2353: Object literal may only specify known properties, and 'c' does not exist in type '{ a: string; b: number; }'.
53

64

75
==== arrayLiterals.ts (2 errors) ====
@@ -30,12 +28,10 @@ arrayLiterals.ts(24,101): error TS2322: Type '{ a: string; b: number; c: number;
3028
// Contextual type C with numeric index signature makes array literal of EveryType E of type BCT(E,C)[]
3129
var context1: { [n: number]: { a: string; b: number; }; } = [{ a: '', b: 0, c: '' }, { a: "", b: 3, c: 0 }];
3230
~
33-
!!! error TS2322: Type '{ a: string; b: number; c: string; }' is not assignable to type '{ a: string; b: number; }'.
34-
!!! error TS2322: Object literal may only specify known properties, and 'c' does not exist in type '{ a: string; b: number; }'.
31+
!!! error TS2353: Object literal may only specify known properties, and 'c' does not exist in type '{ a: string; b: number; }'.
3532
!!! related TS6501 arrayLiterals.ts:24:17: The expected type comes from this index signature.
3633
~
37-
!!! error TS2322: Type '{ a: string; b: number; c: number; }' is not assignable to type '{ a: string; b: number; }'.
38-
!!! error TS2322: Object literal may only specify known properties, and 'c' does not exist in type '{ a: string; b: number; }'.
34+
!!! error TS2353: Object literal may only specify known properties, and 'c' does not exist in type '{ a: string; b: number; }'.
3935
!!! related TS6501 arrayLiterals.ts:24:17: The expected type comes from this index signature.
4036
var context2 = [{ a: '', b: 0, c: '' }, { a: "", b: 3, c: 0 }];
4137

tests/baselines/reference/assignmentCompatBug2.errors.txt

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
assignmentCompatBug2.ts(1,27): error TS2322: Type '{ a: number; }' is not assignable to type '{ b: number; }'.
2-
Object literal may only specify known properties, and 'a' does not exist in type '{ b: number; }'.
3-
assignmentCompatBug2.ts(3,8): error TS2322: Type '{ a: number; }' is not assignable to type '{ b: number; }'.
4-
Object literal may only specify known properties, and 'a' does not exist in type '{ b: number; }'.
5-
assignmentCompatBug2.ts(5,13): error TS2322: Type '{ b: number; a: number; }' is not assignable to type '{ b: number; }'.
6-
Object literal may only specify known properties, and 'a' does not exist in type '{ b: number; }'.
1+
assignmentCompatBug2.ts(1,27): error TS2353: Object literal may only specify known properties, and 'a' does not exist in type '{ b: number; }'.
2+
assignmentCompatBug2.ts(3,8): error TS2353: Object literal may only specify known properties, and 'a' does not exist in type '{ b: number; }'.
3+
assignmentCompatBug2.ts(5,13): error TS2353: Object literal may only specify known properties, and 'a' does not exist in type '{ b: number; }'.
74
assignmentCompatBug2.ts(15,1): error TS2741: Property 'm' is missing in type '{ f: (n: number) => number; g: (s: string) => number; }' but required in type '{ f(n: number): number; g(s: string): number; m: number; n?: number; k?(a: any): any; }'.
85
assignmentCompatBug2.ts(20,1): error TS2741: Property 'g' is missing in type '{ f: (n: number) => number; m: number; }' but required in type '{ f(n: number): number; g(s: string): number; m: number; n?: number; k?(a: any): any; }'.
96
assignmentCompatBug2.ts(33,1): error TS2741: Property 'm' is missing in type '{ f: (n: number) => number; g: (s: string) => number; n: number; k: (a: any) => any; }' but required in type '{ f(n: number): number; g(s: string): number; m: number; n?: number; k?(a: any): any; }'.
@@ -12,18 +9,15 @@ assignmentCompatBug2.ts(33,1): error TS2741: Property 'm' is missing in type '{
129
==== assignmentCompatBug2.ts (6 errors) ====
1310
var b2: { b: number;} = { a: 0 }; // error
1411
~
15-
!!! error TS2322: Type '{ a: number; }' is not assignable to type '{ b: number; }'.
16-
!!! error TS2322: Object literal may only specify known properties, and 'a' does not exist in type '{ b: number; }'.
12+
!!! error TS2353: Object literal may only specify known properties, and 'a' does not exist in type '{ b: number; }'.
1713

1814
b2 = { a: 0 }; // error
1915
~
20-
!!! error TS2322: Type '{ a: number; }' is not assignable to type '{ b: number; }'.
21-
!!! error TS2322: Object literal may only specify known properties, and 'a' does not exist in type '{ b: number; }'.
16+
!!! error TS2353: Object literal may only specify known properties, and 'a' does not exist in type '{ b: number; }'.
2217

2318
b2 = {b: 0, a: 0 };
2419
~
25-
!!! error TS2322: Type '{ b: number; a: number; }' is not assignable to type '{ b: number; }'.
26-
!!! error TS2322: Object literal may only specify known properties, and 'a' does not exist in type '{ b: number; }'.
20+
!!! error TS2353: Object literal may only specify known properties, and 'a' does not exist in type '{ b: number; }'.
2721

2822
var b3: { f(n: number): number; g(s: string): number; m: number; n?: number; k?(a: any): any; };
2923

tests/baselines/reference/assignmentCompatBug5.errors.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
assignmentCompatBug5.ts(2,8): error TS2345: Argument of type '{ b: number; }' is not assignable to parameter of type '{ a: number; }'.
2-
Object literal may only specify known properties, and 'b' does not exist in type '{ a: number; }'.
1+
assignmentCompatBug5.ts(2,8): error TS2353: Object literal may only specify known properties, and 'b' does not exist in type '{ a: number; }'.
32
assignmentCompatBug5.ts(5,7): error TS2322: Type 'string' is not assignable to type 'number'.
43
assignmentCompatBug5.ts(5,12): error TS2322: Type 'string' is not assignable to type 'number'.
54
assignmentCompatBug5.ts(8,6): error TS2345: Argument of type '(s: string) => void' is not assignable to parameter of type '(n: number) => number'.
@@ -13,8 +12,7 @@ assignmentCompatBug5.ts(9,6): error TS2345: Argument of type '(n: number) => voi
1312
function foo1(x: { a: number; }) { }
1413
foo1({ b: 5 });
1514
~
16-
!!! error TS2345: Argument of type '{ b: number; }' is not assignable to parameter of type '{ a: number; }'.
17-
!!! error TS2345: Object literal may only specify known properties, and 'b' does not exist in type '{ a: number; }'.
15+
!!! error TS2353: Object literal may only specify known properties, and 'b' does not exist in type '{ a: number; }'.
1816

1917
function foo2(x: number[]) { }
2018
foo2(["s", "t"]);

tests/baselines/reference/checkJsdocSatisfiesTag1.errors.txt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
/a.js(21,44): error TS1360: Type '{ a: number; b: number; }' does not satisfy the expected type 'T1'.
2-
Object literal may only specify known properties, and 'b' does not exist in type 'T1'.
1+
/a.js(21,44): error TS2353: Object literal may only specify known properties, and 'b' does not exist in type 'T1'.
32
/a.js(22,17): error TS1360: Type '{}' does not satisfy the expected type 'T1'.
43
Property 'a' is missing in type '{}' but required in type 'T1'.
5-
/a.js(31,49): error TS1360: Type '{ a: string; b: string; }' does not satisfy the expected type 'T4'.
6-
Object literal may only specify known properties, and 'b' does not exist in type 'T4'.
4+
/a.js(31,49): error TS2353: Object literal may only specify known properties, and 'b' does not exist in type 'T4'.
75

86

97
==== /a.js (3 errors) ====
@@ -29,8 +27,7 @@
2927
const t1 = /** @satisfies {T1} */ ({ a: 1 });
3028
const t2 = /** @satisfies {T1} */ ({ a: 1, b: 1 });
3129
~
32-
!!! error TS1360: Type '{ a: number; b: number; }' does not satisfy the expected type 'T1'.
33-
!!! error TS1360: Object literal may only specify known properties, and 'b' does not exist in type 'T1'.
30+
!!! error TS2353: Object literal may only specify known properties, and 'b' does not exist in type 'T1'.
3431
const t3 = /** @satisfies {T1} */ ({});
3532
~~~~~~~~~
3633
!!! error TS1360: Type '{}' does not satisfy the expected type 'T1'.
@@ -46,6 +43,5 @@
4643
const t7 = /** @satisfies {T4} */ ({ a: 'test' });
4744
const t8 = /** @satisfies {T4} */ ({ a: 'test', b: 'test' });
4845
~
49-
!!! error TS1360: Type '{ a: string; b: string; }' does not satisfy the expected type 'T4'.
50-
!!! error TS1360: Object literal may only specify known properties, and 'b' does not exist in type 'T4'.
46+
!!! error TS2353: Object literal may only specify known properties, and 'b' does not exist in type 'T4'.
5147

tests/baselines/reference/checkJsdocSatisfiesTag10.errors.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
/a.js(6,5): error TS1360: Type '{ a: number; b: string; x: number; }' does not satisfy the expected type 'Partial<Record<Keys, unknown>>'.
2-
Object literal may only specify known properties, and 'x' does not exist in type 'Partial<Record<Keys, unknown>>'.
1+
/a.js(6,5): error TS2353: Object literal may only specify known properties, and 'x' does not exist in type 'Partial<Record<Keys, unknown>>'.
32
/a.js(14,11): error TS2339: Property 'd' does not exist on type '{ a: number; b: string; x: number; }'.
43

54

@@ -11,8 +10,7 @@
1110
b: "hello",
1211
x: 8 // Should error, 'x' isn't in 'Keys'
1312
~
14-
!!! error TS1360: Type '{ a: number; b: string; x: number; }' does not satisfy the expected type 'Partial<Record<Keys, unknown>>'.
15-
!!! error TS1360: Object literal may only specify known properties, and 'x' does not exist in type 'Partial<Record<Keys, unknown>>'.
13+
!!! error TS2353: Object literal may only specify known properties, and 'x' does not exist in type 'Partial<Record<Keys, unknown>>'.
1614
});
1715

1816
// Should be OK -- retain info that a is number and b is string

tests/baselines/reference/checkJsdocSatisfiesTag12.errors.txt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
/a.js(24,20): error TS1360: Type '{ a: number; b: number; }' does not satisfy the expected type 'T1'.
2-
Object literal may only specify known properties, and 'b' does not exist in type 'T1'.
3-
/a.js(44,25): error TS1360: Type '{ a: string; b: string; }' does not satisfy the expected type 'T2'.
4-
Object literal may only specify known properties, and 'b' does not exist in type 'T2'.
1+
/a.js(24,20): error TS2353: Object literal may only specify known properties, and 'b' does not exist in type 'T1'.
2+
/a.js(44,25): error TS2353: Object literal may only specify known properties, and 'b' does not exist in type 'T2'.
53
/a.js(51,6): error TS1360: Type 'number' does not satisfy the expected type 'string'.
64

75

@@ -31,8 +29,7 @@
3129
*/
3230
const t2 = { a: 1, b: 1 };
3331
~
34-
!!! error TS1360: Type '{ a: number; b: number; }' does not satisfy the expected type 'T1'.
35-
!!! error TS1360: Object literal may only specify known properties, and 'b' does not exist in type 'T1'.
32+
!!! error TS2353: Object literal may only specify known properties, and 'b' does not exist in type 'T1'.
3633

3734
/**
3835
* @satisfies {T1}
@@ -54,8 +51,7 @@
5451
*/
5552
const t6 = { a: 'test', b: 'test' };
5653
~
57-
!!! error TS1360: Type '{ a: string; b: string; }' does not satisfy the expected type 'T2'.
58-
!!! error TS1360: Object literal may only specify known properties, and 'b' does not exist in type 'T2'.
54+
!!! error TS2353: Object literal may only specify known properties, and 'b' does not exist in type 'T2'.
5955

6056
/**
6157
* @satisfies {T3}

0 commit comments

Comments
 (0)