Skip to content

Commit 487ba21

Browse files
authored
Merge pull request #17903 from Microsoft/revert-17870-fix-getConstraintOfIndexedAccess
Revert "Fix getConstraintOfIndexedAccess"
2 parents 439cdca + 6b68da1 commit 487ba21

9 files changed

+8
-334
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5911,13 +5911,7 @@ namespace ts {
59115911
return transformed;
59125912
}
59135913
const baseObjectType = getBaseConstraintOfType(type.objectType);
5914-
const keepTypeParameterForMappedType = baseObjectType && getObjectFlags(baseObjectType) & ObjectFlags.Mapped && type.indexType.flags & TypeFlags.TypeParameter;
5915-
const baseIndexType = !keepTypeParameterForMappedType && getBaseConstraintOfType(type.indexType);
5916-
if (baseObjectType && baseIndexType === stringType && !getIndexInfoOfType(baseObjectType, IndexKind.String)) {
5917-
// getIndexedAccessType returns `any` for X[string] where X doesn't have an index signature.
5918-
// instead, return undefined so that the indexed access check fails
5919-
return undefined;
5920-
}
5914+
const baseIndexType = getBaseConstraintOfType(type.indexType);
59215915
return baseObjectType || baseIndexType ? getIndexedAccessType(baseObjectType || type.objectType, baseIndexType || type.indexType) : undefined;
59225916
}
59235917

@@ -5967,9 +5961,8 @@ namespace ts {
59675961
function computeBaseConstraint(t: Type): Type {
59685962
if (t.flags & TypeFlags.TypeParameter) {
59695963
const constraint = getConstraintFromTypeParameter(<TypeParameter>t);
5970-
return (t as TypeParameter).isThisType || !constraint ?
5971-
constraint :
5972-
getBaseConstraint(constraint);
5964+
return (<TypeParameter>t).isThisType ? constraint :
5965+
constraint ? getBaseConstraint(constraint) : undefined;
59735966
}
59745967
if (t.flags & TypeFlags.UnionOrIntersection) {
59755968
const types = (<UnionOrIntersectionType>t).types;
@@ -5997,6 +5990,9 @@ namespace ts {
59975990
const baseIndexedAccess = baseObjectType && baseIndexType ? getIndexedAccessType(baseObjectType, baseIndexType) : undefined;
59985991
return baseIndexedAccess && baseIndexedAccess !== unknownType ? getBaseConstraint(baseIndexedAccess) : undefined;
59995992
}
5993+
if (isGenericMappedType(t)) {
5994+
return emptyObjectType;
5995+
}
60005996
return t;
60015997
}
60025998
}
@@ -9293,7 +9289,7 @@ namespace ts {
92939289
}
92949290
else if (target.flags & TypeFlags.IndexedAccess) {
92959291
// A type S is related to a type T[K] if S is related to A[K], where K is string-like and
9296-
// A is the apparent type of T.
9292+
// A is the apparent type of S.
92979293
const constraint = getConstraintOfType(<IndexedAccessType>target);
92989294
if (constraint) {
92999295
if (result = isRelatedTo(source, constraint, reportErrors)) {

src/compiler/core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ namespace ts {
718718
export function sum<T extends Record<K, number>, K extends string>(array: T[], prop: K): number {
719719
let result = 0;
720720
for (const v of array) {
721-
// TODO: Remove the following type assertion once the fix for #17069 is merged
721+
// Note: we need the following type assertion because of GH #17069
722722
result += v[prop] as number;
723723
}
724724
return result;

tests/baselines/reference/additionOperatorWithConstrainedTypeParameter.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

tests/baselines/reference/additionOperatorWithConstrainedTypeParameter.symbols

Lines changed: 0 additions & 54 deletions
This file was deleted.

tests/baselines/reference/additionOperatorWithConstrainedTypeParameter.types

Lines changed: 0 additions & 64 deletions
This file was deleted.

tests/baselines/reference/nonPrimitiveConstraintOfIndexAccessType.errors.txt

Lines changed: 0 additions & 67 deletions
This file was deleted.

tests/baselines/reference/nonPrimitiveConstraintOfIndexAccessType.js

Lines changed: 0 additions & 67 deletions
This file was deleted.

tests/cases/conformance/expressions/binaryOperators/additionOperator/additionOperatorWithConstrainedTypeParameter.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

tests/cases/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)