Skip to content

Commit fd1bb9b

Browse files
committed
Group intersection code in getSimplifiedIndexedAccessType
1 parent f1622f0 commit fd1bb9b

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

src/compiler/checker.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8367,33 +8367,35 @@ namespace ts {
83678367
// undefined if no transformation is possible.
83688368
function getSimplifiedIndexedAccessType(type: IndexedAccessType): Type {
83698369
const objectType = type.objectType;
8370-
// Given an indexed access type T[K], if T is an intersection containing one or more generic types and one or
8371-
// more object types with only a string index signature, e.g. '(U & V & { [x: string]: D })[K]', return a
8372-
// transformed type of the form '(U & V)[K] | D'. This allows us to properly reason about higher order indexed
8373-
// access types with default property values as expressed by D.
8374-
if (objectType.flags & TypeFlags.Intersection && isGenericObjectType(objectType) && some((<IntersectionType>objectType).types, isStringIndexOnlyType)) {
8375-
const regularTypes: Type[] = [];
8376-
const stringIndexTypes: Type[] = [];
8377-
for (const t of (<IntersectionType>objectType).types) {
8378-
if (isStringIndexOnlyType(t)) {
8379-
stringIndexTypes.push(getIndexTypeOfType(t, IndexKind.String));
8380-
}
8381-
else {
8382-
regularTypes.push(t);
8370+
if (objectType.flags & TypeFlags.Intersection && isGenericObjectType(objectType)) {
8371+
// Given an indexed access type T[K], if T is an intersection containing one or more generic types and one or
8372+
// more object types with only a string index signature, e.g. '(U & V & { [x: string]: D })[K]', return a
8373+
// transformed type of the form '(U & V)[K] | D'. This allows us to properly reason about higher order indexed
8374+
// access types with default property values as expressed by D.
8375+
if (some((<IntersectionType>objectType).types, isStringIndexOnlyType)) {
8376+
const regularTypes: Type[] = [];
8377+
const stringIndexTypes: Type[] = [];
8378+
for (const t of (<IntersectionType>objectType).types) {
8379+
if (isStringIndexOnlyType(t)) {
8380+
stringIndexTypes.push(getIndexTypeOfType(t, IndexKind.String));
8381+
}
8382+
else {
8383+
regularTypes.push(t);
8384+
}
83838385
}
8386+
return getUnionType([
8387+
getIndexedAccessType(getIntersectionType(regularTypes), type.indexType),
8388+
getIntersectionType(stringIndexTypes)
8389+
]);
8390+
}
8391+
// Given an indexed access type T[K], if T is an intersection containing one or more generic types and one or
8392+
// more mapped types with a template type `never`, '(U & V & { [P in T]: never })[K]', return a
8393+
// transformed type that removes the never-mapped type: '(U & V)[K]'. This mirrors what would happen
8394+
// eventually anyway, but it easier to reason about.
8395+
if (some((<IntersectionType>objectType).types, isMappedTypeToNever)) {
8396+
const nonNeverTypes = filter((<IntersectionType>objectType).types, t => !isMappedTypeToNever(t));
8397+
return getIndexedAccessType(getIntersectionType(nonNeverTypes), type.indexType);
83848398
}
8385-
return getUnionType([
8386-
getIndexedAccessType(getIntersectionType(regularTypes), type.indexType),
8387-
getIntersectionType(stringIndexTypes)
8388-
]);
8389-
}
8390-
// Given an indexed access type T[K], if T is an intersection containing one or more generic types and one or
8391-
// more mapped types with a template type `never`, '(U & V & { [P in T]: never })[K]', return a
8392-
// transformed type that removes the never-mapped type: '(U & V)[K]'. This mirrors what would happen
8393-
// eventually anyway, but it easier to reason about.
8394-
if (objectType.flags & TypeFlags.Intersection && isGenericObjectType(objectType) && some((<IntersectionType>objectType).types, isMappedTypeToNever)) {
8395-
const nonNeverTypes = filter((<IntersectionType>objectType).types, t => !isMappedTypeToNever(t));
8396-
return getIndexedAccessType(getIntersectionType(nonNeverTypes), type.indexType);
83978399
}
83988400

83998401
// If the object type is a mapped type { [P in K]: E }, where K is generic, instantiate E using a mapper

0 commit comments

Comments
 (0)