Skip to content

Commit b8b5948

Browse files
authored
Cache results of isGenericObjectType and isGenericIndexType (#36622)
1 parent de37c87 commit b8b5948

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

Diff for: src/compiler/checker.ts

+19-5
Original file line numberDiff line numberDiff line change
@@ -12498,11 +12498,25 @@ namespace ts {
1249812498
}
1249912499

1250012500
function isGenericObjectType(type: Type): boolean {
12501-
return maybeTypeOfKind(type, TypeFlags.InstantiableNonPrimitive | TypeFlags.GenericMappedType);
12501+
if (type.flags & TypeFlags.UnionOrIntersection) {
12502+
if (!((<UnionOrIntersectionType>type).objectFlags & ObjectFlags.IsGenericObjectTypeComputed)) {
12503+
(<UnionOrIntersectionType>type).objectFlags |= ObjectFlags.IsGenericObjectTypeComputed |
12504+
(some((<UnionOrIntersectionType>type).types, isGenericObjectType) ? ObjectFlags.IsGenericObjectType : 0);
12505+
}
12506+
return !!((<UnionOrIntersectionType>type).objectFlags & ObjectFlags.IsGenericObjectType);
12507+
}
12508+
return !!(type.flags & TypeFlags.InstantiableNonPrimitive) || isGenericMappedType(type);
1250212509
}
1250312510

1250412511
function isGenericIndexType(type: Type): boolean {
12505-
return maybeTypeOfKind(type, TypeFlags.InstantiableNonPrimitive | TypeFlags.Index);
12512+
if (type.flags & TypeFlags.UnionOrIntersection) {
12513+
if (!((<UnionOrIntersectionType>type).objectFlags & ObjectFlags.IsGenericIndexTypeComputed)) {
12514+
(<UnionOrIntersectionType>type).objectFlags |= ObjectFlags.IsGenericIndexTypeComputed |
12515+
(some((<UnionOrIntersectionType>type).types, isGenericIndexType) ? ObjectFlags.IsGenericIndexType : 0);
12516+
}
12517+
return !!((<UnionOrIntersectionType>type).objectFlags & ObjectFlags.IsGenericIndexType);
12518+
}
12519+
return !!(type.flags & (TypeFlags.InstantiableNonPrimitive | TypeFlags.Index));
1250612520
}
1250712521

1250812522
function isThisTypeParameter(type: Type): boolean {
@@ -12726,7 +12740,7 @@ namespace ts {
1272612740
if (checkType === wildcardType || extendsType === wildcardType) {
1272712741
return wildcardType;
1272812742
}
12729-
const checkTypeInstantiable = maybeTypeOfKind(checkType, TypeFlags.Instantiable | TypeFlags.GenericMappedType);
12743+
const checkTypeInstantiable = isGenericObjectType(checkType) || isGenericIndexType(checkType);
1273012744
let combinedMapper: TypeMapper | undefined;
1273112745
if (root.inferTypeParameters) {
1273212746
const context = createInferenceContext(root.inferTypeParameters, /*signature*/ undefined, InferenceFlags.None);
@@ -12745,7 +12759,7 @@ namespace ts {
1274512759
// Instantiate the extends type including inferences for 'infer T' type parameters
1274612760
const inferredExtendsType = combinedMapper ? instantiateType(root.extendsType, combinedMapper) : extendsType;
1274712761
// We attempt to resolve the conditional type only when the check and extends types are non-generic
12748-
if (!checkTypeInstantiable && !maybeTypeOfKind(inferredExtendsType, TypeFlags.Instantiable | TypeFlags.GenericMappedType)) {
12762+
if (!checkTypeInstantiable && !isGenericObjectType(inferredExtendsType) && !isGenericIndexType(inferredExtendsType)) {
1274912763
if (inferredExtendsType.flags & TypeFlags.AnyOrUnknown) {
1275012764
return instantiateType(root.trueType, combinedMapper || mapper);
1275112765
}
@@ -27037,7 +27051,7 @@ namespace ts {
2703727051
// Return true if type might be of the given kind. A union or intersection type might be of a given
2703827052
// kind if at least one constituent type is of the given kind.
2703927053
function maybeTypeOfKind(type: Type, kind: TypeFlags): boolean {
27040-
if (type.flags & kind & ~TypeFlags.GenericMappedType || kind & TypeFlags.GenericMappedType && isGenericMappedType(type)) {
27054+
if (type.flags & kind) {
2704127055
return true;
2704227056
}
2704327057
if (type.flags & TypeFlags.UnionOrIntersection) {

Diff for: src/compiler/types.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -4348,9 +4348,6 @@ namespace ts {
43484348
IncludesWildcard = Index,
43494349
/* @internal */
43504350
IncludesEmptyObject = IndexedAccess,
4351-
// The following flag is used for different purposes by maybeTypeOfKind
4352-
/* @internal */
4353-
GenericMappedType = Never,
43544351
}
43554352

43564353
export type DestructuringPattern = BindingPattern | ObjectLiteralExpression | ArrayLiteralExpression;
@@ -4454,6 +4451,14 @@ namespace ts {
44544451
ContainsObjectOrArrayLiteral = 1 << 20, // Type is or contains object literal type
44554452
/* @internal */
44564453
NonInferrableType = 1 << 21, // Type is or contains anyFunctionType or silentNeverType
4454+
/* @internal */
4455+
IsGenericObjectTypeComputed = 1 << 22, // IsGenericObjectType flag has been computed
4456+
/* @internal */
4457+
IsGenericObjectType = 1 << 23, // Union or intersection contains generic object type
4458+
/* @internal */
4459+
IsGenericIndexTypeComputed = 1 << 24, // IsGenericIndexType flag has been computed
4460+
/* @internal */
4461+
IsGenericIndexType = 1 << 25, // Union or intersection contains generic index type
44574462
ClassOrInterface = Class | Interface,
44584463
/* @internal */
44594464
RequiresWidening = ContainsWideningType | ContainsObjectOrArrayLiteral,

0 commit comments

Comments
 (0)