Skip to content

Cache simplified indexed accesses to better handle circularly constrained indexed acceses #24072

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8692,6 +8692,10 @@ namespace ts {
// Transform an indexed access to a simpler form, if possible. Return the simpler form, or return
// the type itself if no transformation is possible.
function getSimplifiedIndexedAccessType(type: IndexedAccessType): Type {
if (type.simplified) {
return type.simplified === circularConstraintType ? type : type.simplified;
}
type.simplified = circularConstraintType;
const objectType = type.objectType;
if (objectType.flags & TypeFlags.Intersection && isGenericObjectType(objectType)) {
// Given an indexed access type T[K], if T is an intersection containing one or more generic types and one or
Expand All @@ -8709,7 +8713,7 @@ namespace ts {
regularTypes.push(t);
}
}
return getUnionType([
return type.simplified = getUnionType([
getSimplifiedType(getIndexedAccessType(getIntersectionType(regularTypes), type.indexType)),
getIntersectionType(stringIndexTypes)
]);
Expand All @@ -8720,23 +8724,23 @@ namespace ts {
// eventually anyway, but it easier to reason about.
if (some((<IntersectionType>objectType).types, isMappedTypeToNever)) {
const nonNeverTypes = filter((<IntersectionType>objectType).types, t => !isMappedTypeToNever(t));
return getSimplifiedType(getIndexedAccessType(getIntersectionType(nonNeverTypes), type.indexType));
return type.simplified = getSimplifiedType(getIndexedAccessType(getIntersectionType(nonNeverTypes), type.indexType));
}
}
// If the object type is a mapped type { [P in K]: E }, where K is generic, instantiate E using a mapper
// that substitutes the index type for P. For example, for an index access { [P in K]: Box<T[P]> }[X], we
// construct the type Box<T[X]>. We do not further simplify the result because mapped types can be recursive
// and we might never terminate.
if (isGenericMappedType(objectType)) {
return substituteIndexedMappedType(objectType, type);
return type.simplified = substituteIndexedMappedType(objectType, type);
}
if (objectType.flags & TypeFlags.TypeParameter) {
const constraint = getConstraintFromTypeParameter(objectType as TypeParameter);
if (constraint && isGenericMappedType(constraint)) {
return substituteIndexedMappedType(constraint, type);
return type.simplified = substituteIndexedMappedType(constraint, type);
}
}
return type;
return type.simplified = type;
}

function substituteIndexedMappedType(objectType: MappedType, type: IndexedAccessType) {
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3973,6 +3973,7 @@ namespace ts {
objectType: Type;
indexType: Type;
constraint?: Type;
simplified?: Type;
}

export type TypeVariable = TypeParameter | IndexedAccessType;
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2248,6 +2248,7 @@ declare namespace ts {
objectType: Type;
indexType: Type;
constraint?: Type;
simplified?: Type;
}
type TypeVariable = TypeParameter | IndexedAccessType;
interface IndexType extends InstantiableType {
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2248,6 +2248,7 @@ declare namespace ts {
objectType: Type;
indexType: Type;
constraint?: Type;
simplified?: Type;
}
type TypeVariable = TypeParameter | IndexedAccessType;
interface IndexType extends InstantiableType {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//// [circularConstrainedMappedTypeNoCrash.ts]
type Loop<T, U extends Loop<T, U>> = {
[P in keyof T]: U[P] extends boolean ? number : string;
};

//// [circularConstrainedMappedTypeNoCrash.js]
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== tests/cases/compiler/circularConstrainedMappedTypeNoCrash.ts ===
type Loop<T, U extends Loop<T, U>> = {
>Loop : Symbol(Loop, Decl(circularConstrainedMappedTypeNoCrash.ts, 0, 0))
>T : Symbol(T, Decl(circularConstrainedMappedTypeNoCrash.ts, 0, 10))
>U : Symbol(U, Decl(circularConstrainedMappedTypeNoCrash.ts, 0, 12))
>Loop : Symbol(Loop, Decl(circularConstrainedMappedTypeNoCrash.ts, 0, 0))
>T : Symbol(T, Decl(circularConstrainedMappedTypeNoCrash.ts, 0, 10))
>U : Symbol(U, Decl(circularConstrainedMappedTypeNoCrash.ts, 0, 12))

[P in keyof T]: U[P] extends boolean ? number : string;
>P : Symbol(P, Decl(circularConstrainedMappedTypeNoCrash.ts, 1, 5))
>T : Symbol(T, Decl(circularConstrainedMappedTypeNoCrash.ts, 0, 10))
>U : Symbol(U, Decl(circularConstrainedMappedTypeNoCrash.ts, 0, 12))
>P : Symbol(P, Decl(circularConstrainedMappedTypeNoCrash.ts, 1, 5))

};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== tests/cases/compiler/circularConstrainedMappedTypeNoCrash.ts ===
type Loop<T, U extends Loop<T, U>> = {
>Loop : Loop<T, U>
>T : T
>U : U
>Loop : Loop<T, U>
>T : T
>U : U

[P in keyof T]: U[P] extends boolean ? number : string;
>P : P
>T : T
>U : U
>P : P

};
3 changes: 3 additions & 0 deletions tests/cases/compiler/circularConstrainedMappedTypeNoCrash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type Loop<T, U extends Loop<T, U>> = {
[P in keyof T]: U[P] extends boolean ? number : string;
};