public class UnionType extends JSType
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 type String
or of type boolean. 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.
| Modifier and Type | Class and Description |
|---|---|
static class |
UnionType.Builder
Implements type unioning logic, since
UnionTypes only actually need to perform unioning
operations when being (re)built. |
JSType.HasPropertyKind, JSType.Nullability, JSType.SubtypingMode, JSType.TypePairtemplateTypeMap| Modifier and Type | Method and Description |
|---|---|
JSType |
autobox()
Dereferences a type for property access.
|
static UnionType.Builder |
builder(JSTypeRegistry registry)
Creates a
UnionType.Builder for a new UnionType. |
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)
A
UnionType 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 of
this and that. |
BooleanLiteralSet |
getPossibleToBooleanOutcomes()
Computes the set of possible outcomes of the
ToBoolean predicate
for this type. |
JSType.HasPropertyKind |
getPropertyKind(java.lang.String pname,
boolean autobox)
Checks whether the property is present on the object.
|
JSType |
getRestrictedTypeGivenToBooleanOutcome(boolean outcome)
Computes the restricted type of this type knowing that the
ToBoolean predicate has a specific value. |
JSType |
getRestrictedUnion(JSType type)
Returns a more restricted union type than
this one, in which all
subtypes of type have been removed. |
JSType.TypePair |
getTypesUnderEquality(JSType that)
Computes the subset of
this and that types if equality
is observed. |
JSType.TypePair |
getTypesUnderInequality(JSType that)
Computes the subset of
this and that types if inequality
is observed. |
JSType.TypePair |
getTypesUnderShallowInequality(JSType that)
Computes the subset of
this and that types under
shallow inequality. |
boolean |
hasAnyTemplateTypesInternal() |
boolean |
isDict()
Returns true iff
this can be a dict. |
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 the
null value, and therefore can appear in contexts where
null is expected. |
boolean |
isObject()
Tests whether this type is an
Object, or any subtype thereof. |
boolean |
isStruct()
Returns true iff
this can be a struct. |
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 an
Object context, such as the expression in a with
statement. |
boolean |
matchesStringContext()
This predicate is used to test whether a given type can appear in a
String 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 a
Symbol context |
JSType |
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.
|
boolean |
setValidator(com.google.common.base.Predicate<JSType> validator)
Certain types have constraints on them at resolution-time.
|
TernaryValue |
testForEquality(JSType that)
Compares
this and that. |
java.lang.String |
toDebugHashCodeString()
A hash code function for diagnosing complicated issues
around type-identity.
|
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.
|
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, isBooleanObjectType, isBooleanValueType, isBoxableScalar, isCheckedUnknownType, isConstructor, isDateType, isEmptyType, isEnumElementType, isEnumType, isEquivalent, isEquivalentTo, isEquivalentTo, isFunctionPrototypeType, isFunctionType, isGlobalThisType, isInstanceType, isInterface, isLiteralObject, isNamedType, isNativeObjectType, isNominalConstructor, isNominalType, isNoObjectType, isNoResolvedType, isNoType, isNullType, isNumber, isNumberObjectType, isNumberValueType, isObjectType, isOrdinaryFunction, isRawTypeOfTemplatizedType, isRecordType, isRegexpType, isResolved, isSomeUnknownType, isString, isStringObjectType, isStringValueType, isStructuralInterface, isStructuralType, isSubtype, isSubtype, isSubtypeOf, isSubtypeOf, isSubtypeWithoutStructuralTyping, isSuccessfullyResolved, isSymbol, isSymbolObjectType, isSymbolValueType, isTemplateType, isTemplatizedType, isTypeVariable, isUnionType, isUnresolved, isUnresolvedOrResolvedUnknown, isUnsuccessfullyResolved, isVoidType, mergeSupertypeTemplateTypes, resolve, toAnnotationString, toMaybeEnumElementType, toMaybeEnumType, toMaybeFunctionType, toMaybeFunctionType, toMaybeNamedType, toMaybeObjectType, toMaybeRecordType, toMaybeTemplateType, toMaybeTemplatizedType, toObjectType, toString, unboxesTopublic static UnionType.Builder builder(JSTypeRegistry registry)
UnionType.Builder for a new UnionType.public com.google.common.collect.ImmutableList<JSType> getAlternates()
public boolean matchesNumberContext()
matchesNumberContext in class JSTypepublic boolean matchesStringContext()
String 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 to String.
matchesStringContext in class JSTypetrue if not VoidTypepublic boolean matchesSymbolContext()
Symbol contextmatchesSymbolContext in class JSTypetrue if not it maybe a symbol or Symbol objectpublic boolean matchesObjectContext()
Object context, such as the expression in a with
statement.
Most types we will encounter, except notably null, have at least
the potential for converting to Object. 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.
matchesObjectContext in class JSTypetrue if the type is not NullType or
VoidTypeprotected JSType findPropertyTypeWithoutConsideringTemplateTypes(java.lang.String propertyName)
JSTypeSubclasses 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
findPropertyTypeWithoutConsideringTemplateTypes in class JSTypepublic boolean canBeCalled()
JSTypecanBeCalled in class JSTypetrue if this type might be callable.public JSType autobox()
JSTypepublic JSType restrictByNotNullOrUndefined()
JSTyperestrictByNotNullOrUndefined in class JSTypepublic JSType restrictByNotUndefined()
JSTyperestrictByNotUndefined in class JSTypepublic JSType restrictByNotNull()
JSTyperestrictByNotNull in class JSTypepublic TernaryValue testForEquality(JSType that)
JSTypethis and that.testForEquality in class JSTypeTernaryValue.TRUE if the comparison of values of
this type and that always succeed (such as
undefined compared to null)TernaryValue.FALSE if the comparison of values of
this type and that always fails (such as
undefined compared to number)TernaryValue.UNKNOWN if the comparison can succeed or
fail depending on the concrete valuespublic boolean isNullable()
null value, and therefore can appear in contexts where
null is expected.isNullable in class JSTypetrue for everything but Number and
Boolean types.public boolean isVoidable()
isVoidable in class JSTypepublic boolean isExplicitlyVoidable()
isExplicitlyVoidable in class JSTypepublic boolean isUnknownType()
isUnknownType in class JSTypepublic boolean isStruct()
JSTypethis can be a struct.
UnionType overrides the method, assume this is not a union here.public boolean isDict()
JSTypethis can be a dict.
UnionType overrides the method, assume this is not a union here.public JSType getLeastSupertype(JSType that)
JSTypethis and that. The least supertype is the join
(∨) or supremum of both types in the type lattice.
Examples:
number ∨ * = *
number ∨ Object = (number, Object)
Number ∨ Object = Object
getLeastSupertype in class JSTypethis ∨ thatpublic JSType.HasPropertyKind getPropertyKind(java.lang.String pname, boolean autobox)
JSTypegetPropertyKind in class JSTypepname - The property name.autobox - Whether to check for the presents on an autoboxed typepublic UnionType toMaybeUnionType()
JSTypetoMaybeUnionType in class JSTypepublic boolean isObject()
JSTypeObject, or any subtype thereof.public boolean contains(JSType type)
UnionType contains a given type (alternate) iff the member
vector contains it.type - The alternate which might be in this union.true if the alternate is in the unionpublic JSType getRestrictedUnion(JSType type)
this one, in which all
subtypes of type have been removed.Examples:
(number,string) restricted by number is
string(null, EvalError, URIError) restricted by
Error is nulltype - the supertype of the types to remove from this union typepublic JSType getRestrictedTypeGivenToBooleanOutcome(boolean outcome)
JSTypeToBoolean predicate has a specific value. For more information
about the ToBoolean predicate, see
JSType.getPossibleToBooleanOutcomes().getRestrictedTypeGivenToBooleanOutcome in class JSTypeoutcome - the value of the ToBoolean predicatepublic BooleanLiteralSet getPossibleToBooleanOutcomes()
JSTypeToBoolean predicate
for this type. The ToBoolean predicate is defined by the ECMA-262
standard, 3rd edition. Its behavior for simple types can be
summarized by the following table:
| type | result |
|---|---|
undefined | {false} |
null | {false} |
boolean | {true, false} |
number | {true, false} |
string | {true, false} |
Object | {true} |
getPossibleToBooleanOutcomes in class JSTypepublic JSType.TypePair getTypesUnderEquality(JSType that)
JSTypethis and that types if equality
is observed. If a value v1 of type null is equal to a value
v2 of type (undefined,number), we can infer that the
type of v1 is null and the type of v2 is
undefined.getTypesUnderEquality in class JSTypethis as the first
component and the restricted type of that as the second
element. The returned pair is never null even though its
components may be nullpublic JSType.TypePair getTypesUnderInequality(JSType that)
JSTypethis and that types if inequality
is observed. If a value v1 of type number is not equal to a
value v2 of type (undefined,number), we can infer that the
type of v1 is number and the type of v2 is
number as well.getTypesUnderInequality in class JSTypethis as the first
component and the restricted type of that as the second
element. The returned pair is never null even though its
components may be nullpublic JSType.TypePair getTypesUnderShallowInequality(JSType that)
JSTypethis and that types under
shallow inequality.getTypesUnderShallowInequality in class JSTypethis as the first
component and the restricted type of that as the second
element. The returned pair is never null even though its
components may be nullpublic <T> T visit(Visitor<T> visitor)
JSTypepublic java.lang.String toDebugHashCodeString()
JSTypetoDebugHashCodeString in class JSTypepublic boolean setValidator(com.google.common.base.Predicate<JSType> validator)
JSType@extends annotation must be an
object. Clients should inject a validator that emits a warning
if the type does not validate, and return false.setValidator in class JSTypepublic JSType collapseUnion()
JSTypecollapseUnion in class JSTypepublic void matchConstraint(JSType constraint)
JSTypematchConstraint in class JSTypepublic boolean hasAnyTemplateTypesInternal()
Copyright © 2009-2019 Google. All Rights Reserved.