Cache isConstTypeVariable check

This commit is contained in:
Anders Hejlsberg
2022-12-12 15:58:39 -08:00
parent ccf252f8f3
commit f8fd1fd29f
2 changed files with 18 additions and 11 deletions

View File

@@ -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 {

View File

@@ -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 */