Skip to content

Commit 6031cbb

Browse files
committed
Preserve error elaboration for indexed access type assignments
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 errorInfo before reporting the generic "could be instantiated with an arbitrary type" message, discarding the useful elaboration. This change preserves errorInfo 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 #63206
1 parent 4f7b417 commit 6031cbb

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22656,7 +22656,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2265622656
);
2265722657
}
2265822658
else {
22659-
errorInfo = undefined;
22659+
// When the target is an indexed access type (e.g. this["faa"]), preserve any
22660+
// specific error elaboration from the constraint comparison (e.g., missing
22661+
// property or type mismatch errors) rather than discarding them. For plain
22662+
// type parameters, clear the error info as the constraint-based errors may
22663+
// be misleading.
22664+
if (!(target.flags & TypeFlags.IndexedAccess)) {
22665+
errorInfo = undefined;
22666+
}
2266022667
reportError(
2266122668
Diagnostics._0_could_be_instantiated_with_an_arbitrary_type_which_could_be_unrelated_to_1,
2266222669
targetType,

tests/baselines/reference/mappedTypeRelationships.errors.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,23 @@ mappedTypeRelationships.ts(25,5): error TS2536: Type 'K' cannot be used to index
1010
mappedTypeRelationships.ts(26,12): error TS2536: Type 'K' cannot be used to index type 'T'.
1111
mappedTypeRelationships.ts(30,5): error TS2322: Type 'T[keyof T] | undefined' is not assignable to type 'T[keyof T]'.
1212
'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'T[keyof T] | undefined'.
13+
Type 'undefined' is not assignable to type 'T[keyof T]'.
14+
'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'undefined'.
1315
mappedTypeRelationships.ts(35,5): error TS2322: Type 'T[K] | undefined' is not assignable to type 'T[K]'.
1416
'T[K]' could be instantiated with an arbitrary type which could be unrelated to 'T[K] | undefined'.
17+
Type 'undefined' is not assignable to type 'T[K]'.
18+
'T[K]' could be instantiated with an arbitrary type which could be unrelated to 'undefined'.
1519
mappedTypeRelationships.ts(40,5): error TS2322: Type 'U[keyof T] | undefined' is not assignable to type 'T[keyof T]'.
1620
'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'U[keyof T] | undefined'.
21+
Type 'undefined' is not assignable to type 'T[keyof T]'.
22+
'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'undefined'.
1723
mappedTypeRelationships.ts(41,5): error TS2322: Type 'T[keyof T]' is not assignable to type 'U[keyof T] | undefined'.
1824
Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'U[keyof T] | undefined'.
1925
Type 'T[string]' is not assignable to type 'U[keyof T] | undefined'.
2026
mappedTypeRelationships.ts(45,5): error TS2322: Type 'U[K] | undefined' is not assignable to type 'T[K]'.
2127
'T[K]' could be instantiated with an arbitrary type which could be unrelated to 'U[K] | undefined'.
28+
Type 'undefined' is not assignable to type 'T[K]'.
29+
'T[K]' could be instantiated with an arbitrary type which could be unrelated to 'undefined'.
2230
mappedTypeRelationships.ts(46,5): error TS2322: Type 'T[K]' is not assignable to type 'U[K] | undefined'.
2331
Type 'T[keyof T]' is not assignable to type 'U[K] | undefined'.
2432
Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'U[K] | undefined'.
@@ -125,6 +133,8 @@ mappedTypeRelationships.ts(168,5): error TS2322: Type '{ [P in K]: T[P]; }' is n
125133
~~~~
126134
!!! error TS2322: Type 'T[keyof T] | undefined' is not assignable to type 'T[keyof T]'.
127135
!!! error TS2322: 'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'T[keyof T] | undefined'.
136+
!!! error TS2322: Type 'undefined' is not assignable to type 'T[keyof T]'.
137+
!!! error TS2322: 'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'undefined'.
128138
y[k] = x[k];
129139
}
130140

@@ -133,6 +143,8 @@ mappedTypeRelationships.ts(168,5): error TS2322: Type '{ [P in K]: T[P]; }' is n
133143
~~~~
134144
!!! error TS2322: Type 'T[K] | undefined' is not assignable to type 'T[K]'.
135145
!!! error TS2322: 'T[K]' could be instantiated with an arbitrary type which could be unrelated to 'T[K] | undefined'.
146+
!!! error TS2322: Type 'undefined' is not assignable to type 'T[K]'.
147+
!!! error TS2322: 'T[K]' could be instantiated with an arbitrary type which could be unrelated to 'undefined'.
136148
y[k] = x[k];
137149
}
138150

@@ -141,6 +153,8 @@ mappedTypeRelationships.ts(168,5): error TS2322: Type '{ [P in K]: T[P]; }' is n
141153
~~~~
142154
!!! error TS2322: Type 'U[keyof T] | undefined' is not assignable to type 'T[keyof T]'.
143155
!!! error TS2322: 'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'U[keyof T] | undefined'.
156+
!!! error TS2322: Type 'undefined' is not assignable to type 'T[keyof T]'.
157+
!!! error TS2322: 'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'undefined'.
144158
y[k] = x[k]; // Error
145159
~~~~
146160
!!! error TS2322: Type 'T[keyof T]' is not assignable to type 'U[keyof T] | undefined'.
@@ -153,6 +167,8 @@ mappedTypeRelationships.ts(168,5): error TS2322: Type '{ [P in K]: T[P]; }' is n
153167
~~~~
154168
!!! error TS2322: Type 'U[K] | undefined' is not assignable to type 'T[K]'.
155169
!!! error TS2322: 'T[K]' could be instantiated with an arbitrary type which could be unrelated to 'U[K] | undefined'.
170+
!!! error TS2322: Type 'undefined' is not assignable to type 'T[K]'.
171+
!!! error TS2322: 'T[K]' could be instantiated with an arbitrary type which could be unrelated to 'undefined'.
156172
y[k] = x[k]; // Error
157173
~~~~
158174
!!! error TS2322: Type 'T[K]' is not assignable to type 'U[K] | undefined'.

tests/baselines/reference/nonPrimitiveConstraintOfIndexAccessType.errors.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ nonPrimitiveConstraintOfIndexAccessType.ts(12,5): error TS2322: Type 'string' is
88
'T[P]' could be instantiated with an arbitrary type which could be unrelated to 'string'.
99
nonPrimitiveConstraintOfIndexAccessType.ts(15,5): error TS2322: Type 'string' is not assignable to type 'T[P]'.
1010
'T[P]' could be instantiated with an arbitrary type which could be unrelated to 'string'.
11+
Type 'string' is not assignable to type 'never'.
1112
nonPrimitiveConstraintOfIndexAccessType.ts(18,5): error TS2322: Type 'string' is not assignable to type 'T[P]'.
1213
'T[P]' could be instantiated with an arbitrary type which could be unrelated to 'string'.
1314
nonPrimitiveConstraintOfIndexAccessType.ts(21,5): error TS2322: Type 'string' is not assignable to type 'T[P]'.
@@ -51,6 +52,7 @@ nonPrimitiveConstraintOfIndexAccessType.ts(30,5): error TS2322: Type 'string' is
5152
~~
5253
!!! error TS2322: Type 'string' is not assignable to type 'T[P]'.
5354
!!! error TS2322: 'T[P]' could be instantiated with an arbitrary type which could be unrelated to 'string'.
55+
!!! error TS2322: Type 'string' is not assignable to type 'never'.
5456
}
5557
function k<T extends number, P extends keyof T>(s: string, tp: T[P]): void {
5658
tp = s;

0 commit comments

Comments
 (0)