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
Skip parent error when reporting excess property checks #55152
Changes from 6 commits
54994b2
f3bb166
5dee22d
f180889
9551e67
e76b422
4a789d3
f5579bd
64982f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20354,6 +20354,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
let overrideNextErrorInfo = 0; // How many `reportRelationError` calls should be skipped in the elaboration pyramid | ||
let lastSkippedInfo: [Type, Type] | undefined; | ||
let incompatibleStack: DiagnosticAndArguments[] | undefined; | ||
let skipParentCounter = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be captured in Also, maybe a comment on how this differs from the nearby |
||
|
||
Debug.assert(relation !== identityRelation || !errorNode, "no error reporting in identity checking"); | ||
|
||
|
@@ -20544,7 +20545,17 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
Debug.assert(!!errorNode); | ||
if (incompatibleStack) reportIncompatibleStack(); | ||
if (message.elidedInCompatabilityPyramid) return; | ||
errorInfo = chainDiagnosticMessages(errorInfo, message, ...args); | ||
if (skipParentCounter === 0) { | ||
errorInfo = chainDiagnosticMessages(errorInfo, message, ...args); | ||
} | ||
else { | ||
skipParentCounter--; | ||
} | ||
} | ||
|
||
function reportParentSkippedError(message: DiagnosticMessage, ...args: DiagnosticArguments): void { | ||
reportError(message, ...args); | ||
skipParentCounter++; | ||
} | ||
|
||
function associateRelatedInfo(info: DiagnosticRelatedInformation) { | ||
|
@@ -20941,11 +20952,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
} | ||
} | ||
if (suggestion !== undefined) { | ||
reportError(Diagnostics.Object_literal_may_only_specify_known_properties_but_0_does_not_exist_in_type_1_Did_you_mean_to_write_2, | ||
reportParentSkippedError(Diagnostics.Object_literal_may_only_specify_known_properties_but_0_does_not_exist_in_type_1_Did_you_mean_to_write_2, | ||
symbolToString(prop), typeToString(errorTarget), suggestion); | ||
} | ||
else { | ||
reportError(Diagnostics.Object_literal_may_only_specify_known_properties_and_0_does_not_exist_in_type_1, | ||
reportParentSkippedError(Diagnostics.Object_literal_may_only_specify_known_properties_and_0_does_not_exist_in_type_1, | ||
symbolToString(prop), typeToString(errorTarget)); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
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. | ||
Type '{ foo: string; }' is not comparable to type '{ id: number; }'. | ||
Object literal may only specify known properties, and 'foo' does not exist in type '{ id: number; }'. | ||
Object literal may only specify known properties, and 'foo' does not exist in type '{ id: number; }'. | ||
|
||
|
||
==== arrayCast.ts (1 errors) ==== | ||
|
@@ -9,8 +8,7 @@ arrayCast.ts(3,23): error TS2352: Conversion of type '{ foo: string; }[]' to typ | |
<{ id: number; }[]>[{ foo: "s" }]; | ||
~~~ | ||
!!! 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. | ||
!!! error TS2352: Type '{ foo: string; }' is not comparable to type '{ id: number; }'. | ||
!!! error TS2352: Object literal may only specify known properties, and 'foo' does not exist in type '{ id: number; }'. | ||
!!! error TS2352: Object literal may only specify known properties, and 'foo' does not exist in type '{ id: number; }'. | ||
Comment on lines
-13
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This error should never have been issued in the first place. It seems like a bug in our element-wise elaboration logic. Notice how in the following example one of these is has a missing property error and the other is an excess property error: let okay = { foo: "" } as { id: 123 };
// ~~~~~~~~~~~~~~~~~~~~~~~~~~
// Conversion of type '{ foo: string; }' to type '{ id: 123; }' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
// Property 'id' is missing in type '{ foo: string; }' but required in type '{ id: 123; }'.
let waat = [{ foo: "" }] as { id: 123 }[];
// ~~~
// Conversion of type '{ foo: string; }[]' to type '{ id: 123; }[]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
// Type '{ foo: string; }' is not comparable to type '{ id: 123; }'.
// Object literal may only specify known properties, and 'foo' does not exist in type '{ id: 123; }'. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Opened up #55167 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I wouldn’t ever expect an EPC error on a type assertion. |
||
|
||
// Should succeed, as the {} element causes the type of the array to be {}[] | ||
<{ id: number; }[]>[{ foo: "s" }, {}]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This currently never goes higher than 1, but if we ever had the notion of an EPC->EPC nested error, that would be how it could happen. Open to changing it to a boolean if wanted.