Skip to content

Commit b49a343

Browse files
committed
Simplify obtaining base constraint of T[K] for writing
1 parent e1fd5e5 commit b49a343

File tree

1 file changed

+14
-26
lines changed

1 file changed

+14
-26
lines changed

src/compiler/checker.ts

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7236,7 +7236,7 @@ namespace ts {
72367236
// because of constraints on type parameters (e.g. 'keyof T' for a constrained T).
72377237
// The isIndexType flag indicates that the type is the index type of an indexed
72387238
// access that is the target of an assignment.
7239-
function getLowerBoundOfKeyType(type: Type, isIndexType: boolean): Type {
7239+
function getLowerBoundOfKeyType(type: Type): Type {
72407240
if (type.flags & (TypeFlags.Any | TypeFlags.Primitive)) {
72417241
return type;
72427242
}
@@ -7246,7 +7246,7 @@ namespace ts {
72467246
if (type.flags & TypeFlags.Conditional) {
72477247
if ((<ConditionalType>type).root.isDistributive) {
72487248
const checkType = (<ConditionalType>type).checkType;
7249-
const constraint = getLowerBoundOfKeyType(checkType, isIndexType);
7249+
const constraint = getLowerBoundOfKeyType(checkType);
72507250
if (constraint !== checkType) {
72517251
const mapper = makeUnaryTypeMapper((<ConditionalType>type).root.checkType, constraint);
72527252
return getConditionalTypeInstantiation(<ConditionalType>type, combineTypeMappers(mapper, (<ConditionalType>type).mapper));
@@ -7255,13 +7255,10 @@ namespace ts {
72557255
return type;
72567256
}
72577257
if (type.flags & TypeFlags.Union) {
7258-
return getUnionType(sameMap((<UnionType>type).types, t => getLowerBoundOfKeyType(t, isIndexType)));
7258+
return getUnionType(sameMap((<UnionType>type).types, getLowerBoundOfKeyType));
72597259
}
72607260
if (type.flags & TypeFlags.Intersection) {
7261-
return getIntersectionType(sameMap((<UnionType>type).types, t => getLowerBoundOfKeyType(t, isIndexType)));
7262-
}
7263-
if (isIndexType && type.flags & TypeFlags.Instantiable) {
7264-
return getLowerBoundOfKeyType(getConstraintOfType(type) || neverType, isIndexType);
7261+
return getIntersectionType(sameMap((<UnionType>type).types, getLowerBoundOfKeyType));
72657262
}
72667263
return neverType;
72677264
}
@@ -7294,7 +7291,7 @@ namespace ts {
72947291
}
72957292
}
72967293
else {
7297-
forEachType(getLowerBoundOfKeyType(constraintType, /*isIndexType*/ false), addMemberForKeyType);
7294+
forEachType(getLowerBoundOfKeyType(constraintType), addMemberForKeyType);
72987295
}
72997296
setStructuredTypeMembers(type, members, emptyArray, emptyArray, stringIndexInfo, numberIndexInfo);
73007297

@@ -12879,27 +12876,18 @@ namespace ts {
1287912876
}
1288012877
}
1288112878
else if (target.flags & TypeFlags.IndexedAccess) {
12882-
// A type S is related to a type T[K], where T and K aren't both type variables, if S is related to C,
12883-
// where C is the base constraint of T[K]
12879+
// A type S is related to a type T[K] if S is related to C, where C is the base
12880+
// constraint of T[K] for writing.
1288412881
if (relation !== identityRelation) {
1288512882
const objectType = (<IndexedAccessType>target).objectType;
1288612883
const indexType = (<IndexedAccessType>target).indexType;
12887-
if (indexType.flags & TypeFlags.StructuredOrInstantiable) {
12888-
const keyType = getLowerBoundOfKeyType(indexType, /*isIndexType*/ true);
12889-
if (keyType !== indexType && !(keyType.flags & TypeFlags.Never)) {
12890-
const targetType = getIndexedAccessTypeOrUndefined(objectType, keyType, /*accessNode*/ undefined, AccessFlags.Writing);
12891-
if (targetType && (result = isRelatedTo(source, targetType, reportErrors))) {
12892-
return result;
12893-
}
12894-
}
12895-
}
12896-
else {
12897-
const constraint = getConstraintOfType(objectType);
12898-
if (constraint) {
12899-
const targetType = getIndexedAccessTypeOrUndefined(constraint, indexType, /*accessNode*/ undefined, AccessFlags.Writing | AccessFlags.NoIndexSignatures);
12900-
if (targetType && (result = isRelatedTo(source, targetType, reportErrors))) {
12901-
return result;
12902-
}
12884+
const baseObjectType = getBaseConstraintOfType(objectType) || objectType;
12885+
const baseIndexType = getBaseConstraintOfType(indexType) || indexType;
12886+
if (!isGenericObjectType(baseObjectType) && !isGenericIndexType(baseIndexType)) {
12887+
const accessFlags = AccessFlags.Writing | (baseObjectType !== objectType ? AccessFlags.NoIndexSignatures : 0);
12888+
const constraint = getIndexedAccessTypeOrUndefined(baseObjectType, baseIndexType, /*accessNode*/ undefined, accessFlags);
12889+
if (constraint && (result = isRelatedTo(source, constraint, reportErrors))) {
12890+
return result;
1290312891
}
1290412892
}
1290512893
}

0 commit comments

Comments
 (0)