From f8fd1fd29f7975fcc3aeac8675c2cb107da33065 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Mon, 12 Dec 2022 15:58:39 -0800 Subject: [PATCH] Cache isConstTypeVariable check --- src/compiler/checker.ts | 17 +++++++++++------ src/compiler/types.ts | 12 +++++++----- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 12f4b92ed34..b947e842e31 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -13212,11 +13212,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { return hasNonCircularBaseConstraint(typeParameter) ? getConstraintFromTypeParameter(typeParameter) : undefined; } - function isConstTypeVariable(type: Type): boolean { - return !!(type.flags & TypeFlags.TypeParameter && some((type as TypeParameter).symbol?.declarations, d => hasSyntacticModifier(d, ModifierFlags.Const)) || - type.flags & TypeFlags.IndexedAccess && isConstTypeVariable((type as IndexedAccessType).objectType)); - } - function getConstraintOfIndexedAccess(type: IndexedAccessType) { return hasNonCircularBaseConstraint(type) ? getConstraintFromIndexedAccess(type) : undefined; } @@ -17037,6 +17032,15 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { return !!(getGenericObjectFlags(type) & ObjectFlags.IsGenericIndexType); } + function isConstTypeVariable(type: Type): boolean { + return !!(getGenericObjectFlags(type) & ObjectFlags.IsConstTypeVariable); + } + + function isConstTypeVariableWorker(type: Type): boolean { + return !!(type.flags & TypeFlags.TypeParameter && some((type as TypeParameter).symbol?.declarations, d => hasSyntacticModifier(d, ModifierFlags.Const)) || + type.flags & TypeFlags.IndexedAccess && isConstTypeVariableWorker((type as IndexedAccessType).objectType)); + } + function getGenericObjectFlags(type: Type): ObjectFlags { if (type.flags & TypeFlags.UnionOrIntersection) { if (!((type as UnionOrIntersectionType).objectFlags & ObjectFlags.IsGenericTypeComputed)) { @@ -17053,7 +17057,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { return (type as SubstitutionType).objectFlags & ObjectFlags.IsGenericType; } return (type.flags & TypeFlags.InstantiableNonPrimitive || isGenericMappedType(type) || isGenericTupleType(type) ? ObjectFlags.IsGenericObjectType : 0) | - (type.flags & (TypeFlags.InstantiableNonPrimitive | TypeFlags.Index | TypeFlags.TemplateLiteral | TypeFlags.StringMapping) && !isPatternLiteralType(type) ? ObjectFlags.IsGenericIndexType : 0); + (type.flags & (TypeFlags.InstantiableNonPrimitive | TypeFlags.Index | TypeFlags.TemplateLiteral | TypeFlags.StringMapping) && !isPatternLiteralType(type) ? ObjectFlags.IsGenericIndexType : 0) | + (isConstTypeVariableWorker(type) ? ObjectFlags.IsConstTypeVariable : 0); } function getSimplifiedType(type: Type, writing: boolean): Type { diff --git a/src/compiler/types.ts b/src/compiler/types.ts index e6ce574df4a..48dfb897de2 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -5922,22 +5922,24 @@ export const enum ObjectFlags { /** @internal */ IsGenericIndexType = 1 << 23, // Union or intersection contains generic index type /** @internal */ + IsConstTypeVariable = 1 << 24, // Union or intersection contains const type parameter + /** @internal */ IsGenericType = IsGenericObjectType | IsGenericIndexType, // Flags that require TypeFlags.Union /** @internal */ - ContainsIntersections = 1 << 24, // Union contains intersections + ContainsIntersections = 1 << 25, // Union contains intersections /** @internal */ - IsUnknownLikeUnionComputed = 1 << 25, // IsUnknownLikeUnion flag has been computed + IsUnknownLikeUnionComputed = 1 << 26, // IsUnknownLikeUnion flag has been computed /** @internal */ - IsUnknownLikeUnion = 1 << 26, // Union of null, undefined, and empty object type + IsUnknownLikeUnion = 1 << 27, // Union of null, undefined, and empty object type /** @internal */ // Flags that require TypeFlags.Intersection /** @internal */ - IsNeverIntersectionComputed = 1 << 24, // IsNeverLike flag has been computed + IsNeverIntersectionComputed = 1 << 25, // IsNeverLike flag has been computed /** @internal */ - IsNeverIntersection = 1 << 25, // Intersection reduces to never + IsNeverIntersection = 1 << 26, // Intersection reduces to never } /** @internal */