From a0546fc1f709afbbfb459b6496db438bb39f8b7e Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 4 Feb 2020 17:32:11 -0800 Subject: [PATCH] Cache results of isGenericObjectType and isGenericIndexType --- src/compiler/checker.ts | 24 +++++++++++++++++++----- src/compiler/types.ts | 11 ++++++++--- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ad3f3b71f29fb..91f182f696b83 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -12458,11 +12458,25 @@ namespace ts { } function isGenericObjectType(type: Type): boolean { - return maybeTypeOfKind(type, TypeFlags.InstantiableNonPrimitive | TypeFlags.GenericMappedType); + if (type.flags & TypeFlags.UnionOrIntersection) { + if (!((type).objectFlags & ObjectFlags.IsGenericObjectTypeComputed)) { + (type).objectFlags |= ObjectFlags.IsGenericObjectTypeComputed | + (some((type).types, isGenericObjectType) ? ObjectFlags.IsGenericObjectType : 0); + } + return !!((type).objectFlags & ObjectFlags.IsGenericObjectType); + } + return !!(type.flags & TypeFlags.InstantiableNonPrimitive) || isGenericMappedType(type); } function isGenericIndexType(type: Type): boolean { - return maybeTypeOfKind(type, TypeFlags.InstantiableNonPrimitive | TypeFlags.Index); + if (type.flags & TypeFlags.UnionOrIntersection) { + if (!((type).objectFlags & ObjectFlags.IsGenericIndexTypeComputed)) { + (type).objectFlags |= ObjectFlags.IsGenericIndexTypeComputed | + (some((type).types, isGenericIndexType) ? ObjectFlags.IsGenericIndexType : 0); + } + return !!((type).objectFlags & ObjectFlags.IsGenericIndexType); + } + return !!(type.flags & (TypeFlags.InstantiableNonPrimitive | TypeFlags.Index)); } function isThisTypeParameter(type: Type): boolean { @@ -12686,7 +12700,7 @@ namespace ts { if (checkType === wildcardType || extendsType === wildcardType) { return wildcardType; } - const checkTypeInstantiable = maybeTypeOfKind(checkType, TypeFlags.Instantiable | TypeFlags.GenericMappedType); + const checkTypeInstantiable = isGenericObjectType(checkType) || isGenericIndexType(checkType); let combinedMapper: TypeMapper | undefined; if (root.inferTypeParameters) { const context = createInferenceContext(root.inferTypeParameters, /*signature*/ undefined, InferenceFlags.None); @@ -12705,7 +12719,7 @@ namespace ts { // Instantiate the extends type including inferences for 'infer T' type parameters const inferredExtendsType = combinedMapper ? instantiateType(root.extendsType, combinedMapper) : extendsType; // We attempt to resolve the conditional type only when the check and extends types are non-generic - if (!checkTypeInstantiable && !maybeTypeOfKind(inferredExtendsType, TypeFlags.Instantiable | TypeFlags.GenericMappedType)) { + if (!checkTypeInstantiable && !isGenericObjectType(inferredExtendsType) && !isGenericIndexType(inferredExtendsType)) { if (inferredExtendsType.flags & TypeFlags.AnyOrUnknown) { return instantiateType(root.trueType, combinedMapper || mapper); } @@ -26992,7 +27006,7 @@ namespace ts { // Return true if type might be of the given kind. A union or intersection type might be of a given // kind if at least one constituent type is of the given kind. function maybeTypeOfKind(type: Type, kind: TypeFlags): boolean { - if (type.flags & kind & ~TypeFlags.GenericMappedType || kind & TypeFlags.GenericMappedType && isGenericMappedType(type)) { + if (type.flags & kind) { return true; } if (type.flags & TypeFlags.UnionOrIntersection) { diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 9b913c2f17d00..b4cfd3a9005b9 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -4336,9 +4336,6 @@ namespace ts { IncludesWildcard = Index, /* @internal */ IncludesEmptyObject = IndexedAccess, - // The following flag is used for different purposes by maybeTypeOfKind - /* @internal */ - GenericMappedType = Never, } export type DestructuringPattern = BindingPattern | ObjectLiteralExpression | ArrayLiteralExpression; @@ -4442,6 +4439,14 @@ namespace ts { ContainsObjectOrArrayLiteral = 1 << 20, // Type is or contains object literal type /* @internal */ NonInferrableType = 1 << 21, // Type is or contains anyFunctionType or silentNeverType + /* @internal */ + IsGenericObjectTypeComputed = 1 << 22, // IsGenericObjectType flag has been computed + /* @internal */ + IsGenericObjectType = 1 << 23, // Union or intersection contains generic object type + /* @internal */ + IsGenericIndexTypeComputed = 1 << 24, // IsGenericIndexType flag has been computed + /* @internal */ + IsGenericIndexType = 1 << 25, // Union or intersection contains generic index type ClassOrInterface = Class | Interface, /* @internal */ RequiresWidening = ContainsWideningType | ContainsObjectOrArrayLiteral, pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy