Skip to content

Commit 42f4554

Browse files
committed
Compute both ObjectFlags.IsGenericXXXType flags in one go
1 parent 3aac11e commit 42f4554

File tree

2 files changed

+29
-34
lines changed

2 files changed

+29
-34
lines changed

src/compiler/checker.ts

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14857,40 +14857,35 @@ namespace ts {
1485714857
return !!(type.flags & TypeFlags.TemplateLiteral) && every((type as TemplateLiteralType).types, isPatternLiteralPlaceholderType);
1485814858
}
1485914859

14860+
function isGenericType(type: Type): boolean {
14861+
return !!getGenericObjectFlags(type);
14862+
}
14863+
1486014864
function isGenericObjectType(type: Type): boolean {
14861-
if (type.flags & TypeFlags.UnionOrIntersection) {
14862-
if (!((type as UnionOrIntersectionType).objectFlags & ObjectFlags.IsGenericObjectTypeComputed)) {
14863-
(type as UnionOrIntersectionType).objectFlags |= ObjectFlags.IsGenericObjectTypeComputed |
14864-
(some((type as UnionOrIntersectionType).types, isGenericObjectType) ? ObjectFlags.IsGenericObjectType : 0);
14865-
}
14866-
return !!((type as UnionOrIntersectionType).objectFlags & ObjectFlags.IsGenericObjectType);
14867-
}
14868-
if (type.flags & TypeFlags.Substitution) {
14869-
if (!((type as SubstitutionType).objectFlags & ObjectFlags.IsGenericObjectTypeComputed)) {
14870-
(type as SubstitutionType).objectFlags |= ObjectFlags.IsGenericObjectTypeComputed |
14871-
(isGenericObjectType((type as SubstitutionType).substitute) || isGenericObjectType((type as SubstitutionType).baseType) ? ObjectFlags.IsGenericObjectType : 0);
14872-
}
14873-
return !!((type as SubstitutionType).objectFlags & ObjectFlags.IsGenericObjectType);
14874-
}
14875-
return !!(type.flags & TypeFlags.InstantiableNonPrimitive) || isGenericMappedType(type) || isGenericTupleType(type);
14865+
return !!(getGenericObjectFlags(type) & ObjectFlags.IsGenericObjectType);
1487614866
}
1487714867

1487814868
function isGenericIndexType(type: Type): boolean {
14869+
return !!(getGenericObjectFlags(type) & ObjectFlags.IsGenericIndexType);
14870+
}
14871+
14872+
function getGenericObjectFlags(type: Type): ObjectFlags {
1487914873
if (type.flags & TypeFlags.UnionOrIntersection) {
14880-
if (!((type as UnionOrIntersectionType).objectFlags & ObjectFlags.IsGenericIndexTypeComputed)) {
14881-
(type as UnionOrIntersectionType).objectFlags |= ObjectFlags.IsGenericIndexTypeComputed |
14882-
(some((type as UnionOrIntersectionType).types, isGenericIndexType) ? ObjectFlags.IsGenericIndexType : 0);
14874+
if (!((type as UnionOrIntersectionType).objectFlags & ObjectFlags.IsGenericTypeComputed)) {
14875+
(type as UnionOrIntersectionType).objectFlags |= ObjectFlags.IsGenericTypeComputed |
14876+
reduceLeft((type as UnionOrIntersectionType).types, (flags, t) => flags | getGenericObjectFlags(t), 0);
1488314877
}
14884-
return !!((type as UnionOrIntersectionType).objectFlags & ObjectFlags.IsGenericIndexType);
14878+
return (type as UnionOrIntersectionType).objectFlags & ObjectFlags.IsGenericType;
1488514879
}
1488614880
if (type.flags & TypeFlags.Substitution) {
14887-
if (!((type as SubstitutionType).objectFlags & ObjectFlags.IsGenericIndexTypeComputed)) {
14888-
(type as SubstitutionType).objectFlags |= ObjectFlags.IsGenericIndexTypeComputed |
14889-
(isGenericIndexType((type as SubstitutionType).substitute) || isGenericIndexType((type as SubstitutionType).baseType) ? ObjectFlags.IsGenericIndexType : 0);
14881+
if (!((type as SubstitutionType).objectFlags & ObjectFlags.IsGenericTypeComputed)) {
14882+
(type as SubstitutionType).objectFlags |= ObjectFlags.IsGenericTypeComputed |
14883+
getGenericObjectFlags((type as SubstitutionType).substitute) | getGenericObjectFlags((type as SubstitutionType).baseType);
1489014884
}
14891-
return !!((type as SubstitutionType).objectFlags & ObjectFlags.IsGenericIndexType);
14885+
return (type as SubstitutionType).objectFlags & ObjectFlags.IsGenericType;
1489214886
}
14893-
return !!(type.flags & (TypeFlags.InstantiableNonPrimitive | TypeFlags.Index | TypeFlags.TemplateLiteral | TypeFlags.StringMapping)) && !isPatternLiteralType(type);
14887+
return (type.flags & TypeFlags.InstantiableNonPrimitive || isGenericMappedType(type) || isGenericTupleType(type) ? ObjectFlags.IsGenericObjectType : 0) |
14888+
(type.flags & (TypeFlags.InstantiableNonPrimitive | TypeFlags.Index | TypeFlags.TemplateLiteral | TypeFlags.StringMapping) && !isPatternLiteralType(type) ? ObjectFlags.IsGenericIndexType : 0);
1489414889
}
1489514890

1489614891
function isThisTypeParameter(type: Type): boolean {
@@ -15167,7 +15162,7 @@ namespace ts {
1516715162
while (true) {
1516815163
const isUnwrapped = isTypicalNondistributiveConditional(root);
1516915164
const checkType = instantiateType(unwrapNondistributiveConditionalTuple(root, getActualTypeVariable(root.checkType)), mapper);
15170-
const checkTypeInstantiable = isGenericObjectType(checkType) || isGenericIndexType(checkType);
15165+
const checkTypeInstantiable = isGenericType(checkType);
1517115166
const extendsType = instantiateType(unwrapNondistributiveConditionalTuple(root, root.extendsType), mapper);
1517215167
if (checkType === wildcardType || extendsType === wildcardType) {
1517315168
return wildcardType;
@@ -15189,7 +15184,7 @@ namespace ts {
1518915184
// Instantiate the extends type including inferences for 'infer T' type parameters
1519015185
const inferredExtendsType = combinedMapper ? instantiateType(unwrapNondistributiveConditionalTuple(root, root.extendsType), combinedMapper) : extendsType;
1519115186
// We attempt to resolve the conditional type only when the check and extends types are non-generic
15192-
if (!checkTypeInstantiable && !isGenericObjectType(inferredExtendsType) && !isGenericIndexType(inferredExtendsType)) {
15187+
if (!checkTypeInstantiable && !isGenericType(inferredExtendsType)) {
1519315188
// Return falseType for a definitely false extends check. We check an instantiations of the two
1519415189
// types with type parameters mapped to the wildcard type, the most permissive instantiations
1519515190
// possible (the wildcard type is assignable to and from all types). If those are not related,
@@ -24248,7 +24243,7 @@ namespace ts {
2424824243
const contextualType = (isIdentifier(node) || isPropertyAccessExpression(node) || isElementAccessExpression(node)) &&
2424924244
!((isJsxOpeningElement(node.parent) || isJsxSelfClosingElement(node.parent)) && node.parent.tagName === node) &&
2425024245
getContextualType(node, ContextFlags.SkipBindingPatterns);
24251-
return contextualType && !(isGenericObjectType(contextualType) || isGenericIndexType(contextualType));
24246+
return contextualType && !isGenericType(contextualType);
2425224247
}
2425324248

2425424249
function getNarrowableTypeForReference(type: Type, reference: Node, checkMode?: CheckMode) {
@@ -41578,7 +41573,7 @@ namespace ts {
4157841573
return grammarErrorOnNode(parameter.name, Diagnostics.An_index_signature_parameter_must_have_a_type_annotation);
4157941574
}
4158041575
const type = getTypeFromTypeNode(parameter.type);
41581-
if (someType(type, t => !!(t.flags & TypeFlags.StringOrNumberLiteralOrUnique)) || isGenericIndexType(type) || isGenericObjectType(type)) {
41576+
if (someType(type, t => !!(t.flags & TypeFlags.StringOrNumberLiteralOrUnique)) || isGenericType(type)) {
4158241577
return grammarErrorOnNode(parameter.name, Diagnostics.An_index_signature_parameter_type_cannot_be_a_literal_type_or_generic_type_Consider_using_a_mapped_object_type_instead);
4158341578
}
4158441579
if (!everyType(type, isValidIndexKeyType)) {

src/compiler/types.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5263,23 +5263,23 @@ namespace ts {
52635263

52645264
// Flags that require TypeFlags.UnionOrIntersection or TypeFlags.Substitution
52655265
/* @internal */
5266-
IsGenericObjectTypeComputed = 1 << 22, // IsGenericObjectType flag has been computed
5266+
IsGenericTypeComputed = 1 << 22, // IsGenericObjectType flag has been computed
52675267
/* @internal */
52685268
IsGenericObjectType = 1 << 23, // Union or intersection contains generic object type
52695269
/* @internal */
5270-
IsGenericIndexTypeComputed = 1 << 24, // IsGenericIndexType flag has been computed
5270+
IsGenericIndexType = 1 << 24, // Union or intersection contains generic index type
52715271
/* @internal */
5272-
IsGenericIndexType = 1 << 25, // Union or intersection contains generic index type
5272+
IsGenericType = IsGenericObjectType | IsGenericIndexType,
52735273

52745274
// Flags that require TypeFlags.Union
52755275
/* @internal */
5276-
ContainsIntersections = 1 << 26, // Union contains intersections
5276+
ContainsIntersections = 1 << 25, // Union contains intersections
52775277

52785278
// Flags that require TypeFlags.Intersection
52795279
/* @internal */
5280-
IsNeverIntersectionComputed = 1 << 26, // IsNeverLike flag has been computed
5280+
IsNeverIntersectionComputed = 1 << 25, // IsNeverLike flag has been computed
52815281
/* @internal */
5282-
IsNeverIntersection = 1 << 27, // Intersection reduces to never
5282+
IsNeverIntersection = 1 << 26, // Intersection reduces to never
52835283
}
52845284

52855285
/* @internal */

0 commit comments

Comments
 (0)