Skip to content

Commit 87613bc

Browse files
committed
Move to simplify to break less with fewer changes
1 parent 1ff6007 commit 87613bc

File tree

1 file changed

+5
-51
lines changed

1 file changed

+5
-51
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9211,22 +9211,6 @@ namespace ts {
92119211
return maybeTypeOfKind(type, TypeFlags.InstantiableNonPrimitive | TypeFlags.Index);
92129212
}
92139213

9214-
// Return true if the given type is a non-generic object type with a string index signature and no
9215-
// other members.
9216-
function isStringIndexOnlyType(type: Type): boolean {
9217-
if (type.flags & TypeFlags.Object && !isGenericMappedType(type)) {
9218-
const t = resolveStructuredTypeMembers(<ObjectType>type);
9219-
return t.properties.length === 0 &&
9220-
t.callSignatures.length === 0 && t.constructSignatures.length === 0 &&
9221-
!!t.stringIndexInfo && !t.numberIndexInfo;
9222-
}
9223-
return false;
9224-
}
9225-
9226-
function isMappedTypeToNever(type: Type): boolean {
9227-
return !!(getObjectFlags(type) & ObjectFlags.Mapped) && getTemplateTypeFromMappedType(type as MappedType) === neverType;
9228-
}
9229-
92309214
function getSimplifiedType(type: Type): Type {
92319215
return type.flags & TypeFlags.IndexedAccess ? getSimplifiedIndexedAccessType(<IndexedAccessType>type) : type;
92329216
}
@@ -9241,35 +9225,11 @@ namespace ts {
92419225
// We recursively simplify the object type as it may in turn be an indexed access type. For example, with
92429226
// '{ [P in T]: { [Q in U]: number } }[T][U]' we want to first simplify the inner indexed access type.
92439227
const objectType = getSimplifiedType(type.objectType);
9244-
if (objectType.flags & TypeFlags.Intersection && isGenericObjectType(objectType)) {
9245-
// Given an indexed access type T[K], if T is an intersection containing one or more generic types and one or
9246-
// more object types with only a string index signature, e.g. '(U & V & { [x: string]: D })[K]', return a
9247-
// transformed type of the form '(U & V)[K] | D'. This allows us to properly reason about higher order indexed
9248-
// access types with default property values as expressed by D.
9249-
if (some((<IntersectionType>objectType).types, isStringIndexOnlyType)) {
9250-
const regularTypes: Type[] = [];
9251-
const stringIndexTypes: Type[] = [];
9252-
for (const t of (<IntersectionType>objectType).types) {
9253-
if (isStringIndexOnlyType(t)) {
9254-
stringIndexTypes.push(getIndexTypeOfType(t, IndexKind.String)!);
9255-
}
9256-
else {
9257-
regularTypes.push(t);
9258-
}
9259-
}
9260-
return type.simplified = getUnionType([
9261-
getSimplifiedType(getIndexedAccessType(getIntersectionType(regularTypes), type.indexType)),
9262-
getIntersectionType(stringIndexTypes)
9263-
]);
9264-
}
9265-
// Given an indexed access type T[K], if T is an intersection containing one or more generic types and one or
9266-
// more mapped types with a template type `never`, '(U & V & { [P in T]: never })[K]', return a
9267-
// transformed type that removes the never-mapped type: '(U & V)[K]'. This mirrors what would happen
9268-
// eventually anyway, but it easier to reason about.
9269-
if (some((<IntersectionType>objectType).types, isMappedTypeToNever)) {
9270-
const nonNeverTypes = filter((<IntersectionType>objectType).types, t => !isMappedTypeToNever(t));
9271-
return type.simplified = getSimplifiedType(getIndexedAccessType(getIntersectionType(nonNeverTypes), type.indexType));
9272-
}
9228+
if (objectType.flags & TypeFlags.Union) {
9229+
return type.simplified = mapType(objectType, t => getIndexedAccessType(t, type.indexType));
9230+
}
9231+
if (objectType.flags & TypeFlags.Intersection) {
9232+
return type.simplified = getIntersectionType(map((objectType as IntersectionType).types, t => getIndexedAccessType(t, type.indexType)));
92739233
}
92749234
// If the object type is a mapped type { [P in K]: E }, where K is generic, instantiate E using a mapper
92759235
// that substitutes the index type for P. For example, for an index access { [P in K]: Box<T[P]> }[X], we
@@ -9297,12 +9257,6 @@ namespace ts {
92979257
if (objectType === wildcardType || indexType === wildcardType) {
92989258
return wildcardType;
92999259
}
9300-
if (objectType.flags & TypeFlags.Union) {
9301-
return mapType(objectType, t => getIndexedAccessType(t, indexType));
9302-
}
9303-
if (objectType.flags & TypeFlags.Intersection) {
9304-
return getIntersectionType(map((objectType as IntersectionType).types, t => getIndexedAccessType(t, indexType)));
9305-
}
93069260
// If the index type is generic, or if the object type is generic and doesn't originate in an expression,
93079261
// we are performing a higher-order index access where we cannot meaningfully access the properties of the
93089262
// object type. Note that for a generic T and a non-generic K, we eagerly resolve T[K] if it originates in

0 commit comments

Comments
 (0)