Skip to content

Preserve error elaboration for indexed access type assignments#3220

Open
WhiteMinds wants to merge 1 commit intomicrosoft:mainfrom
WhiteMinds:fix/improve-indexed-access-error-elaboration
Open

Preserve error elaboration for indexed access type assignments#3220
WhiteMinds wants to merge 1 commit intomicrosoft:mainfrom
WhiteMinds:fix/improve-indexed-access-error-elaboration

Conversation

@WhiteMinds
Copy link

Fixes microsoft/TypeScript#63206

Note: I understand the current contribution scope is limited to 6.0/7.0 differences and crashes. This is a diagnostic quality improvement rather than a behavioral change — no type-checking logic is altered, only the error message detail level. Happy to hold this until 7.0 if preferred.

Problem

When assigning to an indexed access type (e.g. this["faa"]), reportRelationError unconditionally clears errorChain before reporting the generic "could be instantiated with an arbitrary type" message, discarding the specific elaboration already computed from the constraint comparison (missing properties, type mismatches, etc.).

Fix

Preserve errorChain when the target is an IndexedAccess type, so users see both the generic message and the specific details.

Before

this.foo = {}
// Type '{}' is not assignable to type 'this["faa"]'.
//   'this["faa"]' could be instantiated with an arbitrary type which could be unrelated to '{}'.

After

this.foo = {}
// Type '{}' is not assignable to type 'this["faa"]'.
//   'this["faa"]' could be instantiated with an arbitrary type which could be unrelated to '{}'.
//     Property 'key' is missing in type '{}' but required in type '{ key: number | null; }'.

this.foo = {key: "str"}
// Type '{ key: string; }' is not assignable to type 'this["faa"]'.
//   'this["faa"]' could be instantiated with an arbitrary type which could be unrelated to '{ key: string; }'.
//     Type '{ key: string; }' is not assignable to type '{ key: number | null; }'.
//       Types of property 'key' are incompatible.
//         Type 'string' is not assignable to type 'number'.

Two existing baselines updated to reflect the additional elaboration (no errors removed, only more specific detail added).

When assigning to an indexed access type like this["faa"], the checker
resolves the constraint and performs a structural comparison that
generates specific error messages (e.g., missing properties or type
mismatches). However, reportRelationError unconditionally clears
errorChain before reporting the generic "could be instantiated with an
arbitrary type" message, discarding the useful elaboration.

This change preserves errorChain when the target is an IndexedAccess
type, so users see both the generic message and the specific details
about what went wrong (e.g., "Property 'key' is missing" or "Types of
property 'key' are incompatible").

Fixes microsoft/TypeScript#63206
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant