Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22656,7 +22656,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
);
}
else {
errorInfo = undefined;
// When the target is an indexed access type (e.g. this["faa"]), preserve any
// specific error elaboration from the constraint comparison (e.g., missing
// property or type mismatch errors) rather than discarding them. For plain
// type parameters, clear the error info as the constraint-based errors may
// be misleading.
if (!(target.flags & TypeFlags.IndexedAccess)) {
errorInfo = undefined;
}
reportError(
Diagnostics._0_could_be_instantiated_with_an_arbitrary_type_which_could_be_unrelated_to_1,
targetType,
Expand Down
16 changes: 16 additions & 0 deletions tests/baselines/reference/mappedTypeRelationships.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,23 @@ mappedTypeRelationships.ts(25,5): error TS2536: Type 'K' cannot be used to index
mappedTypeRelationships.ts(26,12): error TS2536: Type 'K' cannot be used to index type 'T'.
mappedTypeRelationships.ts(30,5): error TS2322: Type 'T[keyof T] | undefined' is not assignable to type 'T[keyof T]'.
'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'T[keyof T] | undefined'.
Type 'undefined' is not assignable to type 'T[keyof T]'.
'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'undefined'.
mappedTypeRelationships.ts(35,5): error TS2322: Type 'T[K] | undefined' is not assignable to type 'T[K]'.
'T[K]' could be instantiated with an arbitrary type which could be unrelated to 'T[K] | undefined'.
Type 'undefined' is not assignable to type 'T[K]'.
'T[K]' could be instantiated with an arbitrary type which could be unrelated to 'undefined'.
mappedTypeRelationships.ts(40,5): error TS2322: Type 'U[keyof T] | undefined' is not assignable to type 'T[keyof T]'.
'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'U[keyof T] | undefined'.
Type 'undefined' is not assignable to type 'T[keyof T]'.
'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'undefined'.
mappedTypeRelationships.ts(41,5): error TS2322: Type 'T[keyof T]' is not assignable to type 'U[keyof T] | undefined'.
Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'U[keyof T] | undefined'.
Type 'T[string]' is not assignable to type 'U[keyof T] | undefined'.
mappedTypeRelationships.ts(45,5): error TS2322: Type 'U[K] | undefined' is not assignable to type 'T[K]'.
'T[K]' could be instantiated with an arbitrary type which could be unrelated to 'U[K] | undefined'.
Type 'undefined' is not assignable to type 'T[K]'.
'T[K]' could be instantiated with an arbitrary type which could be unrelated to 'undefined'.
mappedTypeRelationships.ts(46,5): error TS2322: Type 'T[K]' is not assignable to type 'U[K] | undefined'.
Type 'T[keyof T]' is not assignable to type 'U[K] | undefined'.
Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'U[K] | undefined'.
Expand Down Expand Up @@ -125,6 +133,8 @@ mappedTypeRelationships.ts(168,5): error TS2322: Type '{ [P in K]: T[P]; }' is n
~~~~
!!! error TS2322: Type 'T[keyof T] | undefined' is not assignable to type 'T[keyof T]'.
!!! error TS2322: 'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'T[keyof T] | undefined'.
!!! error TS2322: Type 'undefined' is not assignable to type 'T[keyof T]'.
!!! error TS2322: 'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'undefined'.
y[k] = x[k];
}

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

Expand All @@ -141,6 +153,8 @@ mappedTypeRelationships.ts(168,5): error TS2322: Type '{ [P in K]: T[P]; }' is n
~~~~
!!! error TS2322: Type 'U[keyof T] | undefined' is not assignable to type 'T[keyof T]'.
!!! error TS2322: 'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'U[keyof T] | undefined'.
!!! error TS2322: Type 'undefined' is not assignable to type 'T[keyof T]'.
!!! error TS2322: 'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'undefined'.
y[k] = x[k]; // Error
~~~~
!!! error TS2322: Type 'T[keyof T]' is not assignable to type 'U[keyof T] | undefined'.
Expand All @@ -153,6 +167,8 @@ mappedTypeRelationships.ts(168,5): error TS2322: Type '{ [P in K]: T[P]; }' is n
~~~~
!!! error TS2322: Type 'U[K] | undefined' is not assignable to type 'T[K]'.
!!! error TS2322: 'T[K]' could be instantiated with an arbitrary type which could be unrelated to 'U[K] | undefined'.
!!! error TS2322: Type 'undefined' is not assignable to type 'T[K]'.
!!! error TS2322: 'T[K]' could be instantiated with an arbitrary type which could be unrelated to 'undefined'.
y[k] = x[k]; // Error
~~~~
!!! error TS2322: Type 'T[K]' is not assignable to type 'U[K] | undefined'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ nonPrimitiveConstraintOfIndexAccessType.ts(12,5): error TS2322: Type 'string' is
'T[P]' could be instantiated with an arbitrary type which could be unrelated to 'string'.
nonPrimitiveConstraintOfIndexAccessType.ts(15,5): error TS2322: Type 'string' is not assignable to type 'T[P]'.
'T[P]' could be instantiated with an arbitrary type which could be unrelated to 'string'.
Type 'string' is not assignable to type 'never'.
nonPrimitiveConstraintOfIndexAccessType.ts(18,5): error TS2322: Type 'string' is not assignable to type 'T[P]'.
'T[P]' could be instantiated with an arbitrary type which could be unrelated to 'string'.
nonPrimitiveConstraintOfIndexAccessType.ts(21,5): error TS2322: Type 'string' is not assignable to type 'T[P]'.
Expand Down Expand Up @@ -51,6 +52,7 @@ nonPrimitiveConstraintOfIndexAccessType.ts(30,5): error TS2322: Type 'string' is
~~
!!! error TS2322: Type 'string' is not assignable to type 'T[P]'.
!!! error TS2322: 'T[P]' could be instantiated with an arbitrary type which could be unrelated to 'string'.
!!! error TS2322: Type 'string' is not assignable to type 'never'.
}
function k<T extends number, P extends keyof T>(s: string, tp: T[P]): void {
tp = s;
Expand Down