Class JSType
- java.lang.Object
-
- com.google.javascript.rhino.jstype.JSType
-
- All Implemented Interfaces:
java.io.Serializable
- Direct Known Subclasses:
AllType,BigIntType,BooleanType,NullType,NumberType,ObjectType,StringType,SymbolType,UnionType,VoidType
public abstract class JSType extends java.lang.Object implements java.io.SerializableRepresents JavaScript value types.Types are split into two separate families: value types and object types.
A special
UnknownTypeexists to represent a wildcard type on which no information can be gathered. In particular, it can assign to everyone, is a subtype of everyone (and everyone is a subtype of it).If you remove the
UnknownType, the set of types in the type system forms a lattice with theisSubtype(com.google.javascript.rhino.jstype.JSType)relation defining the partial order of types. All types are united at the top of the lattice by theAllTypeand at the bottom by theNoType.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classJSType.HasPropertyKindA tristate value returned from canPropertyBeDefined.static classJSType.NullabilitySpecifies how to express nullability of reference types in annotation strings and error messages.static classJSType.SubtypingModeIn files translated from Java, we typecheck null and undefined loosely.static classJSType.TypePaira data structure that represents a pair of types
-
Field Summary
Fields Modifier and Type Field Description protected TemplateTypeMaptemplateTypeMap
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static booleanareIdentical(JSType a, JSType b)FunctionTypeassertFunctionType()Returns this object cast to FunctionType or throws an exception if it isn't a FunctionType.ObjectTypeassertObjectType()Returns this object cast to ObjectType or throws an exception if it isn't an ObjectType.JSTypeautobox()Dereferences a type for property access.JSTypeautoboxesTo()Turn a scalar type to the corresponding object type.booleancanBeCalled()This predicate is used to test whether a given type can be used as the 'function' in a function call.booleancanCastTo(JSType that)Tests whether values ofthistype can be safely assigned to values ofthattype.booleancanTestForEqualityWith(JSType that)Tests whetherthisandthatare meaningfully comparable.booleancanTestForShallowEqualityWith(JSType that)Tests whetherthisandthatare meaningfully comparable using shallow comparison.JSTypecollapseUnion()Gets the least supertype of this that's not a union.booleancontainsReferenceAncestor(JSType target)This function searchers for a type `target` in the reference chain of ProxyObjectTypesObjectTypedereference()Dereferences a type for property access.booleandiffersFrom(JSType that)Whether this type is meaningfully different fromthattype for the purposes of data flow analysis.booleanequals(java.lang.Object other)JSTypefindPropertyType(java.lang.String propertyName)Coerces this type to an Object type, then gets the type of the property whose name is given.protected JSTypefindPropertyTypeWithoutConsideringTemplateTypes(java.lang.String propertyName)Looks up a property on this type, but without properly replacing any templates in the result.java.lang.StringgetDisplayName()Returns a user meaningful label for the JSType instance.JSTypegetEnumeratedTypeOfEnumElement()JSTypegetGreatestSubtype(JSType that)Gets the greatest subtype ofthisandthat.JSTypegetGreatestSubtypeWithProperty(java.lang.String propName)JSDocInfogetJSDocInfo()Gets the docInfo for this type.JSTypegetLeastSupertype(JSType that)Gets the least supertype ofthisandthat.abstract BooleanLiteralSetgetPossibleToBooleanOutcomes()Computes the set of possible outcomes of theToBooleanpredicate for this type.JSType.HasPropertyKindgetPropertyKind(java.lang.String pname)Checks whether the property is present on the object.JSType.HasPropertyKindgetPropertyKind(java.lang.String pname, boolean autobox)Checks whether the property is present on the object.JSTypegetRestrictedTypeGivenOutcome(Outcome outcome)Computes the restricted type of this type knowing that theToBooleanpredicate has a specific value.intgetTemplateParamCount()Return the number of template parameters declared for this type.TemplateTypeMapgetTemplateTypeMap()Returns the template type map associated with this type.com.google.common.collect.ImmutableList<TemplateType>getTypeParameters()Return, in order, the sequence of type parameters declared for this type.JSType.TypePairgetTypesUnderEquality(JSType that)Computes the subset ofthisandthattypes if equality is observed.JSType.TypePairgetTypesUnderInequality(JSType that)Computes the subset ofthisandthattypes if inequality is observed.JSType.TypePairgetTypesUnderShallowEquality(JSType that)Computes the subset ofthisandthattypes under shallow equality.JSType.TypePairgetTypesUnderShallowInequality(JSType that)Computes the subset ofthisandthattypes under shallow inequality.java.lang.Iterable<JSType>getUnionMembers()booleanhasAnyTemplateTypes()booleanhasDisplayName()inthashCode()Calculates a hash of the object as perObject.hashCode().booleanhasProperty(java.lang.String pname)Checks whether the property is present on the object.booleanisAllType()booleanisArrayType()booleanisBigIntObjectType()booleanisBigIntOrNumber()booleanisBigIntValueType()booleanisBooleanObjectType()booleanisBooleanValueType()booleanisBoxableScalar()booleanisCheckedUnknownType()booleanisConstructor()Whether this type is aFunctionTypethat is a constructor or a named type that points to such a type.booleanisDateType()booleanisDict()Returns true iffthiscan be adict.booleanisEmptyType()booleanisEnumElementType()booleanisEnumType()booleanisExplicitlyVoidable()Tests whether this type explicitly allows undefined, as opposed to ? or *.booleanisFunctionPrototypeType()Whether this is the prototype of a function.booleanisFunctionType()Returns true if toMaybeFunctionType returns a non-null FunctionType.booleanisGlobalThisType()Returns true if this is a global this type.booleanisInstanceType()Whether this type is an Instance object of some constructor.booleanisInterface()Whether this type is aFunctionTypethat is an interface or a named type that points to such a type.booleanisLiteralObject()booleanisNamedType()booleanisNativeObjectType()booleanisNominalConstructorOrInterface()Whether this type is the original constructor of a nominal type.booleanisNominalType()Whether this type is a nominal type (a named instance object or a named enum).booleanisNoObjectType()booleanisNoResolvedType()booleanisNoType()booleanisNullable()Tests whether this type is nullable.booleanisNullType()booleanisNumber()Tests whether the type is a number (value or Object).booleanisNumberObjectType()booleanisNumberValueType()booleanisObject()Tests whether this type is anObject, or any subtype thereof.booleanisObjectType()Tests whether this type is anObject, or any subtype thereof.booleanisOnlyBigInt()Checks whether the type is a bigint and *only* a bigint (value or Object).booleanisOrdinaryFunction()Whether this type is aFunctionTypethat is an ordinary function (i.e.booleanisRawTypeOfTemplatizedType()booleanisRecordType()booleanisRegexpType()booleanisResolved()Returns whether the type has undergone resolution.booleanisSomeUnknownType()booleanisString()Tests whether the type is a string (value or Object).booleanisStringObjectType()booleanisStringValueType()booleanisStruct()Returns true iffthiscan be astruct.booleanisStructuralInterface()booleanisStructuralType()booleanisSubtype(JSType supertype)Checks whetherthisis a subtype ofthat.booleanisSubtype(JSType supertype, JSType.SubtypingMode mode)Deprecated.PreferisSubtype(JSType)instead.booleanisSubtypeOf(JSType supertype)booleanisSubtypeOf(JSType supertype, JSType.SubtypingMode mode)booleanisSubtypeWithoutStructuralTyping(JSType supertype)the logic of this method is similar to isSubtype, except that it does not perform structural interface matchingbooleanisSuccessfullyResolved()Returns whether the type has undergone resolution and resolved to a "useful" type.booleanisSymbol()booleanisSymbolObjectType()booleanisSymbolValueType()booleanisTemplateType()booleanisTemplatizedType()booleanisUnionType()booleanisUnknownType()booleanisUnresolved()booleanisUnresolvedOrResolvedUnknown()booleanisUnsuccessfullyResolved()Returns whether the type has undergone resolution and resolved to a "useless" type.booleanisVoidable()Tests whether this type is voidable.booleanisVoidType()booleanloosenTypecheckingDueToForwardReferencedSupertype()During type definition, was one of the supertypes of this type a forward reference?voidmatchConstraint(JSType constraint)Modify this type so that it matches the specified type.booleanmatchesNumberContext()This predicate is used to test whether a given type can appear in a numeric context, such as an operand of a multiply operator.booleanmatchesObjectContext()This predicate is used to test whether a given type can appear in anObjectcontext, such as the expression in a with statement.booleanmatchesStringContext()This predicate is used to test whether a given type can appear in aStringcontext, such as an operand of a string concat (+) operator.booleanmatchesSymbolContext()This predicate is used to test whether a given type can appear in asymbolcontext such as property access.voidmergeSupertypeTemplateTypes(ObjectType other)Prepends the template type map associated with this type, merging in the keys and values of the specified map.JSTyperesolve(ErrorReporter reporter)Resolve this type in the given scope.JSTyperestrictByNotNull()If this is a union type, returns a union type that does not include the null type.JSTyperestrictByNotNullOrUndefined()If this is a union type, returns a union type that does not include the null or undefined type.JSTyperestrictByNotUndefined()If this is a union type, returns a union type that does not include the undefined type.booleansetValidator(com.google.common.base.Predicate<JSType> validator)Certain types have constraints on them at resolution-time.TernaryValuetestForEquality(JSType that)Comparesthisandthat.java.lang.StringtoAnnotationString(JSType.Nullability nullability)EnumElementTypetoMaybeEnumElementType()Downcasts this to an EnumElementType, or returns null if this is not an EnumElementType.EnumTypetoMaybeEnumType()Downcasts this to an EnumType, or returns null if this is not an EnumType.FunctionTypetoMaybeFunctionType()Downcasts this to a FunctionType, or returns null if this is not a function.static FunctionTypetoMaybeFunctionType(JSType type)Null-safe version of toMaybeFunctionType().NamedTypetoMaybeNamedType()ObjectTypetoMaybeObjectType()RecordTypetoMaybeRecordType()Downcasts this to a RecordType, or returns null if this is not a RecordType.TemplateTypetoMaybeTemplateType()Downcasts this to a TemplateType, or returns null if this is not a function.TemplatizedTypetoMaybeTemplatizedType()Downcasts this to a TemplatizedType, or returns null if this is not a function.UnionTypetoMaybeUnionType()Downcasts this to a UnionType, or returns null if this is not a UnionType.ObjectTypetoObjectType()Casts this to an ObjectType, or returns null if this is not an ObjectType.java.lang.StringtoString()A string representation of this type, suitable for printing in warnings.JSTypeunboxesTo()Turn an object type to its corresponding scalar type.abstract <T> Tvisit(Visitor<T> visitor)Visit this type with the given visitor.
-
-
-
Field Detail
-
templateTypeMap
protected TemplateTypeMap templateTypeMap
-
-
Method Detail
-
getJSDocInfo
public JSDocInfo getJSDocInfo()
Gets the docInfo for this type. By default, documentation cannot be attached to arbitrary types. This must be overridden for programmer-defined types.
-
getDisplayName
public java.lang.String getDisplayName()
Returns a user meaningful label for the JSType instance. For example, Functions and Enums will return their declaration name (if they have one). Some types will not have a meaningful display name. Calls to hasDisplayName() will return true IFF getDisplayName() will return null or a zero length string.- Returns:
- the display name of the type, or null if one is not available
-
hasDisplayName
public boolean hasDisplayName()
- Returns:
- true if the JSType has a user meaningful label.
-
getPropertyKind
public JSType.HasPropertyKind getPropertyKind(java.lang.String pname)
Checks whether the property is present on the object.- Parameters:
pname- The property name.
-
getPropertyKind
public JSType.HasPropertyKind getPropertyKind(java.lang.String pname, boolean autobox)
Checks whether the property is present on the object.- Parameters:
pname- The property name.autobox- Whether to check for the presents on an autoboxed type
-
hasProperty
public final boolean hasProperty(java.lang.String pname)
Checks whether the property is present on the object.- Parameters:
pname- The property name.
-
isNoType
public boolean isNoType()
-
isNoResolvedType
public boolean isNoResolvedType()
-
isUnresolved
public final boolean isUnresolved()
-
isUnresolvedOrResolvedUnknown
public final boolean isUnresolvedOrResolvedUnknown()
-
isNoObjectType
public boolean isNoObjectType()
-
isEmptyType
public final boolean isEmptyType()
-
isNumberObjectType
public boolean isNumberObjectType()
-
isNumberValueType
public boolean isNumberValueType()
-
isBigIntObjectType
public boolean isBigIntObjectType()
-
isBigIntValueType
public boolean isBigIntValueType()
-
isFunctionPrototypeType
public boolean isFunctionPrototypeType()
Whether this is the prototype of a function.
-
isStringObjectType
public boolean isStringObjectType()
-
isSymbolObjectType
public boolean isSymbolObjectType()
-
isStringValueType
public boolean isStringValueType()
-
isSymbolValueType
public boolean isSymbolValueType()
-
isString
public final boolean isString()
Tests whether the type is a string (value or Object).- Returns:
this <: (String, string)
-
isNumber
public final boolean isNumber()
Tests whether the type is a number (value or Object).- Returns:
this <: (Number, number)
-
isSymbol
public final boolean isSymbol()
-
isBigIntOrNumber
public final boolean isBigIntOrNumber()
-
isOnlyBigInt
public final boolean isOnlyBigInt()
Checks whether the type is a bigint and *only* a bigint (value or Object).
-
isArrayType
public boolean isArrayType()
-
isBooleanObjectType
public boolean isBooleanObjectType()
-
isBooleanValueType
public boolean isBooleanValueType()
-
isRegexpType
public boolean isRegexpType()
-
isDateType
public boolean isDateType()
-
isNullType
public boolean isNullType()
-
isVoidType
public boolean isVoidType()
-
isAllType
public boolean isAllType()
-
isUnknownType
public boolean isUnknownType()
-
isSomeUnknownType
public final boolean isSomeUnknownType()
-
isCheckedUnknownType
public boolean isCheckedUnknownType()
-
isUnionType
public final boolean isUnionType()
-
isRawTypeOfTemplatizedType
public final boolean isRawTypeOfTemplatizedType()
-
isStruct
public boolean isStruct()
Returns true iffthiscan be astruct. UnionType overrides the method, assumethisis not a union here.
-
isDict
public boolean isDict()
Returns true iffthiscan be adict. UnionType overrides the method, assumethisis not a union here.
-
containsReferenceAncestor
public final boolean containsReferenceAncestor(JSType target)
This function searchers for a type `target` in the reference chain of ProxyObjectTypes
-
isLiteralObject
public final boolean isLiteralObject()
-
getGreatestSubtypeWithProperty
public JSType getGreatestSubtypeWithProperty(java.lang.String propName)
-
toMaybeUnionType
public UnionType toMaybeUnionType()
Downcasts this to a UnionType, or returns null if this is not a UnionType. Named in honor of Haskell's Maybe type constructor.
-
isGlobalThisType
public final boolean isGlobalThisType()
Returns true if this is a global this type.
-
isFunctionType
public final boolean isFunctionType()
Returns true if toMaybeFunctionType returns a non-null FunctionType.
-
toMaybeFunctionType
public FunctionType toMaybeFunctionType()
Downcasts this to a FunctionType, or returns null if this is not a function.For the purposes of this function, we define a MaybeFunctionType as any type in the sub-lattice { x | LEAST_FUNCTION_TYPE <= x <= GREATEST_FUNCTION_TYPE } This definition excludes bottom types like NoType and NoObjectType.
This definition is somewhat arbitrary and axiomatic, but this is the definition that makes the most sense for the most callers.
-
assertFunctionType
public FunctionType assertFunctionType()
Returns this object cast to FunctionType or throws an exception if it isn't a FunctionType.
-
assertObjectType
public ObjectType assertObjectType()
Returns this object cast to ObjectType or throws an exception if it isn't an ObjectType.
-
toMaybeFunctionType
public static FunctionType toMaybeFunctionType(JSType type)
Null-safe version of toMaybeFunctionType().
-
isEnumElementType
public final boolean isEnumElementType()
-
getEnumeratedTypeOfEnumElement
public final JSType getEnumeratedTypeOfEnumElement()
-
toMaybeEnumElementType
public EnumElementType toMaybeEnumElementType()
Downcasts this to an EnumElementType, or returns null if this is not an EnumElementType.
-
isEnumType
public boolean isEnumType()
-
toMaybeEnumType
public EnumType toMaybeEnumType()
Downcasts this to an EnumType, or returns null if this is not an EnumType.
-
isNamedType
public boolean isNamedType()
-
toMaybeNamedType
public NamedType toMaybeNamedType()
-
isRecordType
public boolean isRecordType()
-
isStructuralInterface
public boolean isStructuralInterface()
-
isStructuralType
public boolean isStructuralType()
-
toMaybeRecordType
public RecordType toMaybeRecordType()
Downcasts this to a RecordType, or returns null if this is not a RecordType.
-
isTemplatizedType
public final boolean isTemplatizedType()
-
toMaybeTemplatizedType
public TemplatizedType toMaybeTemplatizedType()
Downcasts this to a TemplatizedType, or returns null if this is not a function.
-
isTemplateType
public final boolean isTemplateType()
-
toMaybeTemplateType
public TemplateType toMaybeTemplateType()
Downcasts this to a TemplateType, or returns null if this is not a function.
-
hasAnyTemplateTypes
public boolean hasAnyTemplateTypes()
-
getTemplateTypeMap
public TemplateTypeMap getTemplateTypeMap()
Returns the template type map associated with this type.
-
getTypeParameters
public final com.google.common.collect.ImmutableList<TemplateType> getTypeParameters()
Return, in order, the sequence of type parameters declared for this type.In general, this value corresponds to an element for every `@template` declaration on the type definition. It does not include template parameters from superclasses or superinterfaces.
-
getTemplateParamCount
public int getTemplateParamCount()
Return the number of template parameters declared for this type.In general, this value corresponds to the number of `@template` declarations on the type definition. It does not include template parameters from superclasses or superinterfaces.
-
mergeSupertypeTemplateTypes
public void mergeSupertypeTemplateTypes(ObjectType other)
Prepends the template type map associated with this type, merging in the keys and values of the specified map.
-
isObject
public boolean isObject()
Tests whether this type is anObject, or any subtype thereof.- Returns:
this <: Object
-
isObjectType
public final boolean isObjectType()
Tests whether this type is anObject, or any subtype thereof.- Returns:
this <: Object
-
isConstructor
public boolean isConstructor()
Whether this type is aFunctionTypethat is a constructor or a named type that points to such a type.
-
isNominalType
public boolean isNominalType()
Whether this type is a nominal type (a named instance object or a named enum).
-
isNominalConstructorOrInterface
public final boolean isNominalConstructorOrInterface()
Whether this type is the original constructor of a nominal type. Does not include structural constructors.
-
isNativeObjectType
public boolean isNativeObjectType()
-
loosenTypecheckingDueToForwardReferencedSupertype
public boolean loosenTypecheckingDueToForwardReferencedSupertype()
During type definition, was one of the supertypes of this type a forward reference?This is a hack to work around the fact that inheritance chains and template types aren't wired up correctly when this happens, which causes various false positives in checks. The known bugs associated are b/145145406, b/144327372, and b/132980305.
This method should only be used to suppress potential false positives caused by one of the above bugs, in the case where we think suppressing typechecking is preferable to emitting a false positive.
-
isInstanceType
public boolean isInstanceType()
Whether this type is an Instance object of some constructor. Does not necessarily mean this is anInstanceObjectType.
-
isInterface
public boolean isInterface()
Whether this type is aFunctionTypethat is an interface or a named type that points to such a type.
-
isOrdinaryFunction
public boolean isOrdinaryFunction()
Whether this type is aFunctionTypethat is an ordinary function (i.e. not a constructor, nominal interface, or record interface), or a named type that points to such a type.
-
equals
public final boolean equals(@Nullable java.lang.Object other)- Overrides:
equalsin classjava.lang.Object
-
differsFrom
public final boolean differsFrom(JSType that)
Whether this type is meaningfully different fromthattype for the purposes of data flow analysis.This is a trickier check than pure equality, because it has to properly handle unknown types. See
EqMethodfor more info.
-
hashCode
public final int hashCode()
Calculates a hash of the object as perObject.hashCode().This method is unsafe for multi-threaded use. The implementation mutates instance state to prevent recursion and therefore expects sole access.
- Overrides:
hashCodein classjava.lang.Object
-
matchesNumberContext
public boolean matchesNumberContext()
This predicate is used to test whether a given type can appear in a numeric context, such as an operand of a multiply operator.
-
matchesStringContext
public boolean matchesStringContext()
This predicate is used to test whether a given type can appear in aStringcontext, such as an operand of a string concat (+) operator. All types have at least the potential for converting toString. When we add externally defined types, such as a browser OM, we may choose to add types that do not automatically convert toString.
-
matchesSymbolContext
public boolean matchesSymbolContext()
This predicate is used to test whether a given type can appear in asymbolcontext such as property access.
-
matchesObjectContext
public boolean matchesObjectContext()
This predicate is used to test whether a given type can appear in anObjectcontext, such as the expression in a with statement. Most types we will encounter, except notablynull, have at least the potential for converting toObject. Host defined objects can get peculiar.
-
findPropertyType
@Nullable public final JSType findPropertyType(java.lang.String propertyName)
Coerces this type to an Object type, then gets the type of the property whose name is given.Unlike
ObjectType.getPropertyType(java.lang.String), returns null if the property is not found.- Returns:
- The property's type.
nullif the current type cannot have properties, or if the type is not found.
-
findPropertyTypeWithoutConsideringTemplateTypes
@ForOverride @Nullable protected JSType findPropertyTypeWithoutConsideringTemplateTypes(java.lang.String propertyName)
Looks up a property on this type, but without properly replacing any templates in the result.Subclasses can override this if they need more complicated logic for property lookup than just autoboxing to an object.
This is only for use by
findPropertyType(JSType). Call that method instead if you need to lookup a property on a random JSType
-
canBeCalled
public boolean canBeCalled()
This predicate is used to test whether a given type can be used as the 'function' in a function call.- Returns:
trueif this type might be callable.
-
canCastTo
public final boolean canCastTo(JSType that)
Tests whether values ofthistype can be safely assigned to values ofthattype.The default implementation verifies that
thisis a subtype ofthat.
-
autoboxesTo
public JSType autoboxesTo()
Turn a scalar type to the corresponding object type.- Returns:
- the auto-boxed type or
nullif this type is not a scalar.
-
isBoxableScalar
public boolean isBoxableScalar()
-
unboxesTo
public JSType unboxesTo()
Turn an object type to its corresponding scalar type.- Returns:
- the unboxed type or
nullif this type does not unbox.
-
toObjectType
public ObjectType toObjectType()
Casts this to an ObjectType, or returns null if this is not an ObjectType. If this is a scalar type, it will *not* be converted to an object type. If you want to simulate JS autoboxing or dereferencing, you should use autoboxesTo() or dereference().
-
autobox
public JSType autobox()
Dereferences a type for property access. Filters null/undefined and autoboxes the resulting type. Never returns null.
-
dereference
@Nullable public final ObjectType dereference()
Dereferences a type for property access. Filters null/undefined, autoboxes the resulting type, and returns it iff it's an object. If not an object, returns null.
-
canTestForEqualityWith
public final boolean canTestForEqualityWith(JSType that)
Tests whetherthisandthatare meaningfully comparable. By meaningfully, we mean compatible types that do not lead to step 22 of the definition of the Abstract Equality Comparison Algorithm (11.9.3, page 55–56) of the ECMA-262 specification.
-
testForEquality
public TernaryValue testForEquality(JSType that)
Comparesthisandthat.- Returns:
TernaryValue.TRUEif the comparison of values ofthistype andthatalways succeed (such asundefinedcompared tonull)TernaryValue.FALSEif the comparison of values ofthistype andthatalways fails (such asundefinedcompared tonumber)TernaryValue.UNKNOWNif the comparison can succeed or fail depending on the concrete values
-
canTestForShallowEqualityWith
public final boolean canTestForShallowEqualityWith(JSType that)
Tests whetherthisandthatare meaningfully comparable using shallow comparison. By meaningfully, we mean compatible types that are not rejected by step 1 of the definition of the Strict Equality Comparison Algorithm (11.9.6, page 56–57) of the ECMA-262 specification.
-
isNullable
public boolean isNullable()
Tests whether this type is nullable.
-
isVoidable
public boolean isVoidable()
Tests whether this type is voidable.
-
isExplicitlyVoidable
public boolean isExplicitlyVoidable()
Tests whether this type explicitly allows undefined, as opposed to ? or *. This is required for a property to be optional.
-
collapseUnion
public JSType collapseUnion()
Gets the least supertype of this that's not a union.
-
getLeastSupertype
public JSType getLeastSupertype(JSType that)
Gets the least supertype ofthisandthat. The least supertype is the join (∨) or supremum of both types in the type lattice.Examples:
number ∨ *=*number ∨ Object=(number, Object)Number ∨ Object=Object
- Returns:
this ∨ that
-
getGreatestSubtype
public JSType getGreatestSubtype(JSType that)
Gets the greatest subtype ofthisandthat. The greatest subtype is the meet (∧) or infimum of both types in the type lattice.Examples
Number ∧ Any=Anynumber ∧ Object=AnyNumber ∧ Object=Number
- Returns:
this ∨ that
-
getRestrictedTypeGivenOutcome
public JSType getRestrictedTypeGivenOutcome(Outcome outcome)
Computes the restricted type of this type knowing that theToBooleanpredicate has a specific value. For more information about theToBooleanpredicate, seegetPossibleToBooleanOutcomes().- Parameters:
outcome- the value of theToBooleanpredicate- Returns:
- the restricted type, or the Any Type if the underlying type could not have yielded this ToBoolean value TODO(user): Move this method to the SemanticRAI and use the visit method of types to get the restricted type.
-
getPossibleToBooleanOutcomes
public abstract BooleanLiteralSet getPossibleToBooleanOutcomes()
Computes the set of possible outcomes of theToBooleanpredicate for this type. TheToBooleanpredicate is defined by the ECMA-262 standard, 3rd edition. Its behavior for simple types can be summarized by the following table:ToBoolean results by input type type result undefined{false} null{false} boolean{true, false} number{true, false} string{true, false} Object{true} - Returns:
- the set of boolean literals for this type
-
getTypesUnderEquality
public JSType.TypePair getTypesUnderEquality(JSType that)
Computes the subset ofthisandthattypes if equality is observed. If a valuev1of typenullis equal to a valuev2of type(undefined,number), we can infer that the type ofv1isnulland the type ofv2isundefined.- Returns:
- a pair containing the restricted type of
thisas the first component and the restricted type ofthatas the second element. The returned pair is nevernulleven though its components may benull
-
getTypesUnderInequality
public JSType.TypePair getTypesUnderInequality(JSType that)
Computes the subset ofthisandthattypes if inequality is observed. If a valuev1of typenumberis not equal to a valuev2of type(undefined,number), we can infer that the type ofv1isnumberand the type ofv2isnumberas well.- Returns:
- a pair containing the restricted type of
thisas the first component and the restricted type ofthatas the second element. The returned pair is nevernulleven though its components may benull
-
getTypesUnderShallowEquality
public final JSType.TypePair getTypesUnderShallowEquality(JSType that)
Computes the subset ofthisandthattypes under shallow equality.- Returns:
- a pair containing the restricted type of
thisas the first component and the restricted type ofthatas the second element. The returned pair is nevernulleven though its components may benull.
-
getTypesUnderShallowInequality
public JSType.TypePair getTypesUnderShallowInequality(JSType that)
Computes the subset ofthisandthattypes under shallow inequality.- Returns:
- A pair containing the restricted type of
thisas the first component and the restricted type ofthatas the second element. The returned pair is nevernulleven though its components may benull
-
getUnionMembers
public java.lang.Iterable<JSType> getUnionMembers()
-
restrictByNotNullOrUndefined
public JSType restrictByNotNullOrUndefined()
If this is a union type, returns a union type that does not include the null or undefined type.
-
restrictByNotUndefined
public JSType restrictByNotUndefined()
If this is a union type, returns a union type that does not include the undefined type.
-
restrictByNotNull
public JSType restrictByNotNull()
If this is a union type, returns a union type that does not include the null type.
-
isSubtypeWithoutStructuralTyping
public final boolean isSubtypeWithoutStructuralTyping(JSType supertype)
the logic of this method is similar to isSubtype, except that it does not perform structural interface matchingThis function is added for disambiguate properties, and is deprecated for the other use cases.
-
isSubtype
public final boolean isSubtype(JSType supertype)
Checks whetherthisis a subtype ofthat.Note this function also returns true if this type structurally matches the protocol define by that type (if that type is an interface function type)
Subtyping rules:
- (unknown) — every type is a subtype of the Unknown type.
- (no) — the No type is a subtype of every type.
- (no-object) — the NoObject type is a subtype of every object type (i.e. subtypes of the Object type).
- (ref) — a type is a subtype of itself.
- (union-l) — A union type is a subtype of a type U if all the union type's
constituents are a subtype of U. Formally
(T1, …, Tn) <: Uif and onlyTk <: Ufor allk ∈ 1..n. - (union-r) — A type U is a subtype of a union type if it is a subtype of one of the
union type's constituents. Formally
U <: (T1, …, Tn)if and only ifU <: Tkfor some indexk. - (objects) — an Object
O1is a subtype of an objectO2if it has more properties thanO2and all common properties are pairwise subtypes.
- Returns:
this <: that
-
isSubtype
@Deprecated public final boolean isSubtype(JSType supertype, JSType.SubtypingMode mode)
Deprecated.PreferisSubtype(JSType)instead.
-
isSubtypeOf
public final boolean isSubtypeOf(JSType supertype)
-
isSubtypeOf
public final boolean isSubtypeOf(JSType supertype, JSType.SubtypingMode mode)
-
visit
public abstract <T> T visit(Visitor<T> visitor)
Visit this type with the given visitor.- Returns:
- the value returned by the visitor
- See Also:
Visitor
-
resolve
public final JSType resolve(ErrorReporter reporter)
Resolve this type in the given scope.The returned value must be equal to
this, as defined byequals(java.lang.Object). It may or may not be the same object. This method may modify the internal state ofthis, as long as it does so in a way that preserves Object equality.For efficiency, we should only resolve a type once per compilation job. For incremental compilations, one compilation job may need the artifacts from a previous generation, so we will eventually need a generational flag instead of a boolean one.
-
isResolved
public final boolean isResolved()
Returns whether the type has undergone resolution.A value of
truedoes not indicate that resolution was successful, only that it was attempted and has finished.
-
isSuccessfullyResolved
public final boolean isSuccessfullyResolved()
Returns whether the type has undergone resolution and resolved to a "useful" type.
-
isUnsuccessfullyResolved
public final boolean isUnsuccessfullyResolved()
Returns whether the type has undergone resolution and resolved to a "useless" type.
-
setValidator
public boolean setValidator(com.google.common.base.Predicate<JSType> validator)
Certain types have constraints on them at resolution-time. For example, a type in an@extendsannotation must be an object. Clients should inject a validator that emits a warning if the type does not validate, and return false.
-
toString
public java.lang.String toString()
A string representation of this type, suitable for printing in warnings.- Overrides:
toStringin classjava.lang.Object
-
toAnnotationString
public final java.lang.String toAnnotationString(JSType.Nullability nullability)
-
matchConstraint
public void matchConstraint(JSType constraint)
Modify this type so that it matches the specified type. This is useful for reverse type-inference, where we want to infer that an object literal matches its constraint (much like how the java compiler does reverse-inference to figure out generics).- Parameters:
constraint-
-
toMaybeObjectType
public ObjectType toMaybeObjectType()
-
-