Skip to content

Commit

Permalink
Cache isConstTypeVariable check
Browse files Browse the repository at this point in the history
  • Loading branch information
ahejlsberg committed Dec 12, 2022
1 parent ccf252f commit f8fd1fd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
17 changes: 11 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)) {
Expand All @@ -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 {
Expand Down
12 changes: 7 additions & 5 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit f8fd1fd

Please sign in to comment.