Skip to content

Commit b876211

Browse files
committed
Property handle union/intersection types in type variable checks
1 parent a230cb7 commit b876211

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

src/compiler/checker.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4523,7 +4523,7 @@ namespace ts {
45234523
// First, if the constraint type is a type parameter, obtain the base constraint. Then,
45244524
// if the key type is a 'keyof X', obtain 'keyof C' where C is the base constraint of X.
45254525
// 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;
45274527
const iterationType = keyType.flags & TypeFlags.Index ? getIndexType(getApparentType((<IndexType>keyType).type)) : keyType;
45284528
forEachType(iterationType, t => {
45294529
// Create a mapper from T to the current iteration type constituent. Then, if the
@@ -4579,7 +4579,7 @@ namespace ts {
45794579
function isGenericMappedType(type: Type) {
45804580
if (getObjectFlags(type) & ObjectFlags.Mapped) {
45814581
const constraintType = getConstraintTypeFromMappedType(<MappedType>type);
4582-
return !!(constraintType.flags & (TypeFlags.TypeParameter | TypeFlags.Index));
4582+
return maybeTypeOfKind(constraintType, TypeFlags.TypeVariable | TypeFlags.Index);
45834583
}
45844584
return false;
45854585
}
@@ -5912,7 +5912,7 @@ namespace ts {
59125912
return links.resolvedType;
59135913
}
59145914

5915-
function getIndexTypeForTypeVariable(type: TypeVariable) {
5915+
function getIndexTypeForGenericType(type: TypeVariable | UnionOrIntersectionType) {
59165916
if (!type.resolvedIndexType) {
59175917
type.resolvedIndexType = <IndexType>createType(TypeFlags.Index);
59185918
type.resolvedIndexType.type = type;
@@ -5931,7 +5931,7 @@ namespace ts {
59315931
}
59325932

59335933
function getIndexType(type: Type): Type {
5934-
return type.flags & TypeFlags.TypeVariable ? getIndexTypeForTypeVariable(<TypeVariable>type) :
5934+
return maybeTypeOfKind(type, TypeFlags.TypeVariable) ? getIndexTypeForGenericType(<TypeVariable | UnionOrIntersectionType>type) :
59355935
getObjectFlags(type) & ObjectFlags.Mapped ? getConstraintTypeFromMappedType(<MappedType>type) :
59365936
type.flags & TypeFlags.Any || getIndexInfoOfType(type, IndexKind.String) ? stringType :
59375937
getLiteralTypeFromPropertyNames(type);
@@ -6032,14 +6032,11 @@ namespace ts {
60326032
}
60336033

60346034
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.
60436040
if (accessNode) {
60446041
if (!isTypeAssignableTo(indexType, getIndexType(objectType))) {
60456042
error(accessNode, Diagnostics.Type_0_cannot_be_used_to_index_type_1, typeToString(indexType), typeToString(objectType));
@@ -6539,7 +6536,7 @@ namespace ts {
65396536
// union type A | undefined, we produce { [P in keyof A]: X } | undefined.
65406537
const constraintType = getConstraintTypeFromMappedType(type);
65416538
if (constraintType.flags & TypeFlags.Index) {
6542-
const typeVariable = <TypeParameter>(<IndexType>constraintType).type;
6539+
const typeVariable = (<IndexType>constraintType).type;
65436540
const mappedTypeVariable = instantiateType(typeVariable, mapper);
65446541
if (typeVariable !== mappedTypeVariable) {
65456542
return mapType(mappedTypeVariable, t => {

src/compiler/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2908,6 +2908,8 @@ namespace ts {
29082908
/* @internal */
29092909
resolvedProperties: SymbolTable; // Cache of resolved properties
29102910
/* @internal */
2911+
resolvedIndexType: IndexType;
2912+
/* @internal */
29112913
couldContainTypeVariables: boolean;
29122914
}
29132915

@@ -2991,7 +2993,7 @@ namespace ts {
29912993

29922994
// keyof T types (TypeFlags.Index)
29932995
export interface IndexType extends Type {
2994-
type: TypeVariable;
2996+
type: TypeVariable | UnionOrIntersectionType;
29952997
}
29962998

29972999
export const enum SignatureKind {

0 commit comments

Comments
 (0)