Class UnionType
- java.lang.Object
-
- com.google.javascript.rhino.jstype.JSType
-
- com.google.javascript.rhino.jstype.UnionType
-
- All Implemented Interfaces:
java.io.Serializable
public class UnionType extends JSType
A type that may be any one of a set of types, and thus has the intersection of the properties of those types.The
UnionType
implements a common JavaScript idiom in which the code is specifically designed to work with multiple input types. Because JavaScript always knows the run-time type of an object value, this is safer than a C union.For instance, values of the union type
(String,boolean)
can be of typeString
or of typeboolean
. The commutativity of the statement is captured by making(String,boolean)
and(boolean,String)
equal.The implementation of this class prevents the creation of nested unions.
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
UnionType.Builder
Implements type unioning logic, sinceUnionType
s only actually need to perform unioning operations when being (re)built.-
Nested classes/interfaces inherited from class com.google.javascript.rhino.jstype.JSType
JSType.HasPropertyKind, JSType.Nullability, JSType.SubtypingMode, JSType.TypePair
-
-
Field Summary
-
Fields inherited from class com.google.javascript.rhino.jstype.JSType
templateTypeMap
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description JSType
autobox()
Dereferences a type for property access.static UnionType.Builder
builder(JSTypeRegistry registry)
Creates aUnionType.Builder
for a newUnionType
.boolean
canBeCalled()
This predicate is used to test whether a given type can be used as the 'function' in a function call.JSType
collapseUnion()
Gets the least supertype of this that's not a union.boolean
contains(JSType type)
AUnionType
contains a given type (alternate) iff the member vector contains it.protected JSType
findPropertyTypeWithoutConsideringTemplateTypes(java.lang.String propertyName)
Looks up a property on this type, but without properly replacing any templates in the result.com.google.common.collect.ImmutableList<JSType>
getAlternates()
Gets the alternate types of this union type.JSType
getLeastSupertype(JSType that)
Gets the least supertype ofthis
andthat
.BooleanLiteralSet
getPossibleToBooleanOutcomes()
Computes the set of possible outcomes of theToBoolean
predicate for this type.JSType.HasPropertyKind
getPropertyKind(java.lang.String pname, boolean autobox)
Checks whether the property is present on the object.JSType
getRestrictedTypeGivenOutcome(Outcome outcome)
Computes the restricted type of this type knowing that theToBoolean
predicate has a specific value.JSType
getRestrictedUnion(JSType type)
Returns a more restricted union type thanthis
one, in which all subtypes oftype
have been removed.JSType.TypePair
getTypesUnderEquality(JSType that)
Computes the subset ofthis
andthat
types if equality is observed.JSType.TypePair
getTypesUnderInequality(JSType that)
Computes the subset ofthis
andthat
types if inequality is observed.JSType.TypePair
getTypesUnderShallowInequality(JSType that)
Computes the subset ofthis
andthat
types under shallow inequality.boolean
hasAnyTemplateTypesInternal()
boolean
isDict()
Returns true iffthis
can be adict
.boolean
isExplicitlyVoidable()
Tests whether this type explicitly allows undefined (as opposed to ? or *).boolean
isNullable()
This predicate determines whether objects of this type can have thenull
value, and therefore can appear in contexts wherenull
is expected.boolean
isObject()
Tests whether this type is anObject
, or any subtype thereof.boolean
isStruct()
Returns true iffthis
can be astruct
.boolean
isUnknownType()
boolean
isVoidable()
Tests whether this type is voidable.void
matchConstraint(JSType constraint)
Modify this type so that it matches the specified type.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.boolean
matchesObjectContext()
This predicate is used to test whether a given type can appear in anObject
context, such as the expression in awith
statement.boolean
matchesStringContext()
This predicate is used to test whether a given type can appear in aString
context, such as an operand of a string concat (+
) operator.boolean
matchesSymbolContext()
This predicate is used to test whether a given type can appear in aSymbol
contextJSType
restrictByNotNull()
If this is a union type, returns a union type that does not include the null type.JSType
restrictByNotNullOrUndefined()
If this is a union type, returns a union type that does not include the null or undefined type.JSType
restrictByNotUndefined()
If this is a union type, returns a union type that does not include the undefined type.TernaryValue
testForEquality(JSType that)
Comparesthis
andthat
.UnionType
toMaybeUnionType()
Downcasts this to a UnionType, or returns null if this is not a UnionType.<T> T
visit(Visitor<T> visitor)
Visit this type with the given visitor.-
Methods inherited from class com.google.javascript.rhino.jstype.JSType
areIdentical, assertFunctionType, assertObjectType, autoboxesTo, canCastTo, canTestForEqualityWith, canTestForShallowEqualityWith, containsReferenceAncestor, dereference, differsFrom, equals, findPropertyType, getDisplayName, getEnumeratedTypeOfEnumElement, getGreatestSubtype, getGreatestSubtypeWithProperty, getJSDocInfo, getPropertyKind, getTemplateParamCount, getTemplateTypeMap, getTypeParameters, getTypesUnderShallowEquality, getUnionMembers, hasAnyTemplateTypes, hasDisplayName, hashCode, hasProperty, isAllType, isArrayType, isBigIntObjectType, isBigIntOrNumber, isBigIntValueType, isBooleanObjectType, isBooleanValueType, isBoxableScalar, isCheckedUnknownType, isConstructor, isDateType, isEmptyType, isEnumElementType, isEnumType, isFunctionPrototypeType, isFunctionType, isGlobalThisType, isInstanceType, isInterface, isLiteralObject, isNamedType, isNativeObjectType, isNominalConstructor, isNominalType, isNoObjectType, isNoResolvedType, isNoType, isNullType, isNumber, isNumberObjectType, isNumberValueType, isObjectType, isOnlyBigInt, isOrdinaryFunction, isRawTypeOfTemplatizedType, isRecordType, isRegexpType, isResolved, isSomeUnknownType, isString, isStringObjectType, isStringValueType, isStructuralInterface, isStructuralType, isSubtype, isSubtype, isSubtypeOf, isSubtypeOf, isSubtypeWithoutStructuralTyping, isSuccessfullyResolved, isSymbol, isSymbolObjectType, isSymbolValueType, isTemplateType, isTemplatizedType, isUnionType, isUnresolved, isUnresolvedOrResolvedUnknown, isUnsuccessfullyResolved, isVoidType, loosenTypecheckingDueToForwardReferencedSupertype, mergeSupertypeTemplateTypes, resolve, setValidator, toAnnotationString, toMaybeEnumElementType, toMaybeEnumType, toMaybeFunctionType, toMaybeFunctionType, toMaybeNamedType, toMaybeObjectType, toMaybeRecordType, toMaybeTemplateType, toMaybeTemplatizedType, toObjectType, toString, unboxesTo
-
-
-
-
Method Detail
-
builder
public static UnionType.Builder builder(JSTypeRegistry registry)
Creates aUnionType.Builder
for a newUnionType
.
-
getAlternates
public com.google.common.collect.ImmutableList<JSType> getAlternates()
Gets the alternate types of this union type.- Returns:
- The alternate types of this union type. The returned set is immutable.
-
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.- Overrides:
matchesNumberContext
in classJSType
- Returns:
- true if the type can appear in a numeric context.
-
matchesStringContext
public boolean matchesStringContext()
This predicate is used to test whether a given type can appear in aString
context, such as an operand of a string concat (+
) operator.All types have at least the potential for converting to
String
. When we add externally defined types, such as a browser OM, we may choose to add types that do not automatically convert toString
.- Overrides:
matchesStringContext
in classJSType
- Returns:
true
if notVoidType
-
matchesSymbolContext
public boolean matchesSymbolContext()
This predicate is used to test whether a given type can appear in aSymbol
context- Overrides:
matchesSymbolContext
in classJSType
- Returns:
true
if not it maybe a symbol or Symbol object
-
matchesObjectContext
public boolean matchesObjectContext()
This predicate is used to test whether a given type can appear in anObject
context, such as the expression in awith
statement.Most types we will encounter, except notably
null
, have at least the potential for converting toObject
. Host defined objects can get peculiar.VOID type is included here because while it is not part of the JavaScript language, functions returning 'void' type can't be used as operands of any operator or statement.
- Overrides:
matchesObjectContext
in classJSType
- Returns:
true
if the type is notNullType
orVoidType
-
findPropertyTypeWithoutConsideringTemplateTypes
protected JSType findPropertyTypeWithoutConsideringTemplateTypes(java.lang.String propertyName)
Description copied from class:JSType
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- Overrides:
findPropertyTypeWithoutConsideringTemplateTypes
in classJSType
-
canBeCalled
public boolean canBeCalled()
Description copied from class:JSType
This predicate is used to test whether a given type can be used as the 'function' in a function call.- Overrides:
canBeCalled
in classJSType
- Returns:
true
if this type might be callable.
-
autobox
public JSType autobox()
Description copied from class:JSType
Dereferences a type for property access. Filters null/undefined and autoboxes the resulting type. Never returns null.
-
restrictByNotNullOrUndefined
public JSType restrictByNotNullOrUndefined()
Description copied from class:JSType
If this is a union type, returns a union type that does not include the null or undefined type.- Overrides:
restrictByNotNullOrUndefined
in classJSType
-
restrictByNotUndefined
public JSType restrictByNotUndefined()
Description copied from class:JSType
If this is a union type, returns a union type that does not include the undefined type.- Overrides:
restrictByNotUndefined
in classJSType
-
restrictByNotNull
public JSType restrictByNotNull()
Description copied from class:JSType
If this is a union type, returns a union type that does not include the null type.- Overrides:
restrictByNotNull
in classJSType
-
testForEquality
public TernaryValue testForEquality(JSType that)
Description copied from class:JSType
Comparesthis
andthat
.- Overrides:
testForEquality
in classJSType
- Returns:
TernaryValue.TRUE
if the comparison of values ofthis
type andthat
always succeed (such asundefined
compared tonull
)TernaryValue.FALSE
if the comparison of values ofthis
type andthat
always fails (such asundefined
compared tonumber
)TernaryValue.UNKNOWN
if the comparison can succeed or fail depending on the concrete values
-
isNullable
public boolean isNullable()
This predicate determines whether objects of this type can have thenull
value, and therefore can appear in contexts wherenull
is expected.- Overrides:
isNullable
in classJSType
- Returns:
true
for everything butNumber
andBoolean
types.
-
isVoidable
public boolean isVoidable()
Tests whether this type is voidable.- Overrides:
isVoidable
in classJSType
-
isExplicitlyVoidable
public boolean isExplicitlyVoidable()
Tests whether this type explicitly allows undefined (as opposed to ? or *).- Overrides:
isExplicitlyVoidable
in classJSType
-
isUnknownType
public boolean isUnknownType()
- Overrides:
isUnknownType
in classJSType
-
isStruct
public boolean isStruct()
Description copied from class:JSType
Returns true iffthis
can be astruct
. UnionType overrides the method, assumethis
is not a union here.
-
isDict
public boolean isDict()
Description copied from class:JSType
Returns true iffthis
can be adict
. UnionType overrides the method, assumethis
is not a union here.
-
getLeastSupertype
public JSType getLeastSupertype(JSType that)
Description copied from class:JSType
Gets the least supertype ofthis
andthat
. The least supertype is the join (∨) or supremum of both types in the type lattice.Examples:
number ∨ *
=*
number ∨ Object
=(number, Object)
Number ∨ Object
=Object
- Overrides:
getLeastSupertype
in classJSType
- Returns:
this ∨ that
-
getPropertyKind
public JSType.HasPropertyKind getPropertyKind(java.lang.String pname, boolean autobox)
Description copied from class:JSType
Checks whether the property is present on the object.- Overrides:
getPropertyKind
in classJSType
- Parameters:
pname
- The property name.autobox
- Whether to check for the presents on an autoboxed type
-
toMaybeUnionType
public UnionType toMaybeUnionType()
Description copied from class:JSType
Downcasts this to a UnionType, or returns null if this is not a UnionType. Named in honor of Haskell's Maybe type constructor.- Overrides:
toMaybeUnionType
in classJSType
-
isObject
public boolean isObject()
Description copied from class:JSType
Tests whether this type is anObject
, or any subtype thereof.
-
contains
public boolean contains(JSType type)
AUnionType
contains a given type (alternate) iff the member vector contains it.- Parameters:
type
- The alternate which might be in this union.- Returns:
true
if the alternate is in the union
-
getRestrictedUnion
public JSType getRestrictedUnion(JSType type)
Returns a more restricted union type thanthis
one, in which all subtypes oftype
have been removed.Examples:
(number,string)
restricted bynumber
isstring
(null, EvalError, URIError)
restricted byError
isnull
- Parameters:
type
- the supertype of the types to remove from this union type
-
getRestrictedTypeGivenOutcome
public JSType getRestrictedTypeGivenOutcome(Outcome outcome)
Description copied from class:JSType
Computes the restricted type of this type knowing that theToBoolean
predicate has a specific value. For more information about theToBoolean
predicate, seeJSType.getPossibleToBooleanOutcomes()
.- Overrides:
getRestrictedTypeGivenOutcome
in classJSType
- Parameters:
outcome
- the value of theToBoolean
predicate- 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 BooleanLiteralSet getPossibleToBooleanOutcomes()
Description copied from class:JSType
Computes the set of possible outcomes of theToBoolean
predicate for this type. TheToBoolean
predicate 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} - Specified by:
getPossibleToBooleanOutcomes
in classJSType
- Returns:
- the set of boolean literals for this type
-
getTypesUnderEquality
public JSType.TypePair getTypesUnderEquality(JSType that)
Description copied from class:JSType
Computes the subset ofthis
andthat
types if equality is observed. If a valuev1
of typenull
is equal to a valuev2
of type(undefined,number)
, we can infer that the type ofv1
isnull
and the type ofv2
isundefined
.- Overrides:
getTypesUnderEquality
in classJSType
- Returns:
- a pair containing the restricted type of
this
as the first component and the restricted type ofthat
as the second element. The returned pair is nevernull
even though its components may benull
-
getTypesUnderInequality
public JSType.TypePair getTypesUnderInequality(JSType that)
Description copied from class:JSType
Computes the subset ofthis
andthat
types if inequality is observed. If a valuev1
of typenumber
is not equal to a valuev2
of type(undefined,number)
, we can infer that the type ofv1
isnumber
and the type ofv2
isnumber
as well.- Overrides:
getTypesUnderInequality
in classJSType
- Returns:
- a pair containing the restricted type of
this
as the first component and the restricted type ofthat
as the second element. The returned pair is nevernull
even though its components may benull
-
getTypesUnderShallowInequality
public JSType.TypePair getTypesUnderShallowInequality(JSType that)
Description copied from class:JSType
Computes the subset ofthis
andthat
types under shallow inequality.- Overrides:
getTypesUnderShallowInequality
in classJSType
- Returns:
- A pair containing the restricted type of
this
as the first component and the restricted type ofthat
as the second element. The returned pair is nevernull
even though its components may benull
-
visit
public <T> T visit(Visitor<T> visitor)
Description copied from class:JSType
Visit this type with the given visitor.
-
collapseUnion
public JSType collapseUnion()
Description copied from class:JSType
Gets the least supertype of this that's not a union.- Overrides:
collapseUnion
in classJSType
-
matchConstraint
public void matchConstraint(JSType constraint)
Description copied from class:JSType
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).- Overrides:
matchConstraint
in classJSType
-
hasAnyTemplateTypesInternal
public boolean hasAnyTemplateTypesInternal()
-
-