@@ -4523,7 +4523,7 @@ namespace ts {
4523
4523
// First, if the constraint type is a type parameter, obtain the base constraint. Then,
4524
4524
// if the key type is a 'keyof X', obtain 'keyof C' where C is the base constraint of X.
4525
4525
// Finally, iterate over the constituents of the resulting iteration type.
4526
- const keyType = constraintType.flags & TypeFlags.TypeParameter ? getApparentType(constraintType) : constraintType;
4526
+ const keyType = constraintType.flags & TypeFlags.TypeVariable ? getApparentType(constraintType) : constraintType;
4527
4527
const iterationType = keyType.flags & TypeFlags.Index ? getIndexType(getApparentType((<IndexType>keyType).type)) : keyType;
4528
4528
forEachType(iterationType, t => {
4529
4529
// Create a mapper from T to the current iteration type constituent. Then, if the
@@ -4579,7 +4579,7 @@ namespace ts {
4579
4579
function isGenericMappedType(type: Type) {
4580
4580
if (getObjectFlags(type) & ObjectFlags.Mapped) {
4581
4581
const constraintType = getConstraintTypeFromMappedType(<MappedType>type);
4582
- return !! (constraintType.flags & ( TypeFlags.TypeParameter | TypeFlags.Index) );
4582
+ return maybeTypeOfKind (constraintType, TypeFlags.TypeVariable | TypeFlags.Index);
4583
4583
}
4584
4584
return false;
4585
4585
}
@@ -5912,7 +5912,7 @@ namespace ts {
5912
5912
return links.resolvedType;
5913
5913
}
5914
5914
5915
- function getIndexTypeForTypeVariable (type: TypeVariable) {
5915
+ function getIndexTypeForGenericType (type: TypeVariable | UnionOrIntersectionType ) {
5916
5916
if (!type.resolvedIndexType) {
5917
5917
type.resolvedIndexType = <IndexType>createType(TypeFlags.Index);
5918
5918
type.resolvedIndexType.type = type;
@@ -5931,7 +5931,7 @@ namespace ts {
5931
5931
}
5932
5932
5933
5933
function getIndexType(type: Type): Type {
5934
- return type.flags & TypeFlags.TypeVariable ? getIndexTypeForTypeVariable (<TypeVariable>type) :
5934
+ return maybeTypeOfKind( type, TypeFlags.TypeVariable) ? getIndexTypeForGenericType (<TypeVariable | UnionOrIntersectionType >type) :
5935
5935
getObjectFlags(type) & ObjectFlags.Mapped ? getConstraintTypeFromMappedType(<MappedType>type) :
5936
5936
type.flags & TypeFlags.Any || getIndexInfoOfType(type, IndexKind.String) ? stringType :
5937
5937
getLiteralTypeFromPropertyNames(type);
@@ -6032,14 +6032,11 @@ namespace ts {
6032
6032
}
6033
6033
6034
6034
function getIndexedAccessType(objectType: Type, indexType: Type, accessNode?: ElementAccessExpression | IndexedAccessTypeNode) {
6035
- if (indexType.flags & TypeFlags.TypeVariable ||
6036
- objectType.flags & TypeFlags.TypeVariable && indexType.flags & TypeFlags.Index ||
6037
- isGenericMappedType(objectType)) {
6038
- // If the object type is a type variable (a type parameter or another indexed access type), if the
6039
- // index type is a type variable or an index type, or if the object type is a mapped type with a
6040
- // generic constraint, we are performing a higher-order index access where we cannot meaningfully
6041
- // access the properties of the object type. In those cases, we first check that the index type is
6042
- // assignable to 'keyof T' for the object type.
6035
+ if (maybeTypeOfKind(indexType, TypeFlags.TypeVariable | TypeFlags.Index) || isGenericMappedType(objectType)) {
6036
+ // If the index type is generic or if the object type is a mapped type with a generic constraint,
6037
+ // we are performing a higher-order index access where we cannot meaningfully access the properties
6038
+ // of the object type. In those cases, we first check that the index type is assignable to 'keyof T'
6039
+ // for the object type.
6043
6040
if (accessNode) {
6044
6041
if (!isTypeAssignableTo(indexType, getIndexType(objectType))) {
6045
6042
error(accessNode, Diagnostics.Type_0_cannot_be_used_to_index_type_1, typeToString(indexType), typeToString(objectType));
@@ -6539,7 +6536,7 @@ namespace ts {
6539
6536
// union type A | undefined, we produce { [P in keyof A]: X } | undefined.
6540
6537
const constraintType = getConstraintTypeFromMappedType(type);
6541
6538
if (constraintType.flags & TypeFlags.Index) {
6542
- const typeVariable = <TypeParameter> (<IndexType>constraintType).type;
6539
+ const typeVariable = (<IndexType>constraintType).type;
6543
6540
const mappedTypeVariable = instantiateType(typeVariable, mapper);
6544
6541
if (typeVariable !== mappedTypeVariable) {
6545
6542
return mapType(mappedTypeVariable, t => {
0 commit comments