Class ObjectType
- Direct Known Subclasses:
EnumElementType,PrototypeObjectType,ProxyObjectType,UnknownType
In JavaScript, all object types have properties, and each of those properties has a type. Property types may be DECLARED, INFERRED, or UNKNOWN.
DECLARED properties have an explicit type annotation, as in:
/xx @type {number} x/
Foo.prototype.bar = 1;
This property may only hold number values, and an assignment to any other type of value
is an error.
INFERRED properties do not have an explicit type annotation. Rather, we try to find all the
possible types that this property can hold.
Foo.prototype.bar = 1;
If the programmer assigns other types of values to this property, the property will take
on the union of all these types.
UNKNOWN properties are properties on the UNKNOWN type. The UNKNOWN type has all properties, but we do not know whether they are declared or inferred.
-
Nested Class Summary
Nested classes/interfaces inherited from class com.google.javascript.rhino.jstype.JSType
JSType.HasPropertyKind, JSType.Nullability, JSType.SubtypingMode, JSType.TypePair, JSType.WithSourceRef -
Field Summary
Fields inherited from class com.google.javascript.rhino.jstype.JSType
templateTypeMap -
Method Summary
Modifier and TypeMethodDescriptionstatic @Nullable ObjectTypeA null-safe version of JSType#toObjectType.voidClear cached values.static StringcreateDelegateSuffix(String suffix) Creates a suffix for a proxy delegate.final booleandefineDeclaredProperty(String propertyName, JSType type, Node propertyNode) Defines a property whose type is explicitly declared by the programmer.final booleandefineInferredProperty(String propertyName, JSType type, Node propertyNode) Defines a property whose type is inferred.final booleandefineSynthesizedProperty(String propertyName, JSType type, Node propertyNode) Defines a property whose type is on a synthesized object.final Property.OwnedPropertyfindClosestDefinition(String propertyName) Returns the closest definition of the property including this type itself.protected @Nullable JSTypefindPropertyTypeWithoutConsideringTemplateTypes(String propertyName) Looks up a property on this type, but without properly replacing any templates in the result.final @Nullable ObjectTypegetClosestDefiningType(String propertyName) Returns the closest ancestor that defines the property including this type itself.abstract FunctionTypeGets this object's constructor.Gets the interfaces extended by the interface associated with this type.Gets the interfaces implemented by the ctor associated with this type.Returns a user meaningful label for the JSType instance.abstract ObjectTypeGets the implicit prototype (a.k.a.final Iterable<ObjectType> Returns a lazy, dynamicIterablefor the types forming the implicit prototype chain of this type.Gets the docInfo for this type.final @Nullable StringDue to the complexity of some of our internal type systems, sometimes we have different types constructed by the same constructor.final @Nullable NodegetOwnPropertyDefSite(String propertyName) final @Nullable JSDocInfogetOwnPropertyJSDocInfo(String propertyName) Gets the docInfo on the specified property on this type.final JSType.HasPropertyKindgetOwnPropertyKind(String propertyName) Checks whether the property whose name is given is present directly on the object.Returns the names of all the properties directly on this type.final PropertygetOwnSlot(String name) Computes the set of possible outcomes of theToBooleanpredicate for this type.final intGets the number of properties of this object.final NodegetPropertyDefSite(String propertyName) final @Nullable JSDocInfogetPropertyJSDocInfo(String propertyName) getPropertyKind(String propertyName, boolean autobox) Checks whether the property is present on the object.final com.google.common.collect.ImmutableSortedSet<String> Returns a list of properties defined or inferred on this type and any of its supertypes.final @Nullable NodegetPropertyNode(String propertyName) Gets the node corresponding to the definition of the specified property.getPropertyType(String propertyName) Gets the property type of the property whose name is given.get the map of properties to types covered in an object typefinal ObjectTypeabstract @Nullable StringGets the reference name for this object.@Nullable PropertyDefault getSlot implementation.@Nullable FunctionTypecom.google.common.collect.ImmutableList<JSType> Gets the declared default element type.booleanReturns true if any cached values have been set for this type.final booleanhasOwnDeclaredProperty(String name) Whether the given property is declared on this object.final booleanhasOwnProperty(String propertyName) Checks whether the property whose name is given is present directly on the object.final booleanINVARIANT:hasReferenceName()is true if and only ifgetReferenceName()returns a non-null string.final booleanWhether this is the prototype of a function.booleanWhether this is a built-in object.booleanisObject()Tests whether this type is anObject, or any subtype thereof.final booleanisPropertyInExterns(String propertyName) Checks whether the property was defined in the externs.final booleanisPropertyTypeDeclared(String propertyName) Checks whether the property's type is declared.final booleanisPropertyTypeInferred(String propertyName) Checks whether the property's type is inferred.booleanbooleanWe treat this as the unknown type if any of its implicit prototype properties is unknown.voidsetJSDocInfo(JSDocInfo info) Sets the docInfo for this type from the givenJSDocInfo.voidsetPropertyJSDocInfo(String propertyName, JSDocInfo info) Sets the docInfo for the specified property from theJSDocInfoon its definition.voidsetPropertyNode(String propertyName, Node defSite) Sets the node where the property was defined.testForEquality(JSType that) Comparesthisandthat.<T> TVisit this type with the given visitor.Methods inherited from class com.google.javascript.rhino.jstype.JSType
areSimilar, assertFunctionType, assertObjectType, autobox, autoboxesTo, canBeCalled, canCastTo, canTestForEqualityWith, canTestForShallowEqualityWith, collapseUnion, containsReferenceAncestor, dereference, differsFrom, equals, findPropertyType, getEnumeratedTypeOfEnumElement, getGreatestSubtype, getLeastSupertype, getPropertyKind, getRestrictedTypeGivenOutcome, getTemplateParamCount, getTemplateTypeMap, getTypeParameters, getTypesUnderEquality, getTypesUnderInequality, getTypesUnderShallowEquality, getTypesUnderShallowInequality, getUnionMembers, hasAnyTemplateTypes, hasDisplayName, hashCode, hasProperty, isAllType, isArrayType, isBigIntObjectType, isBigIntOrNumber, isBigIntValueType, isBooleanObjectType, isBooleanValueType, isBoxableScalar, isCheckedUnknownType, isConstructor, isDateType, isDict, isEmptyType, isEnumElementType, isEnumType, isExplicitlyVoidable, isFunctionType, isGlobalThisType, isInstanceType, isInterface, isLiteralObject, isNamedType, isNominalConstructorOrInterface, isNominalType, isNoObjectType, isNoResolvedType, isNoType, isNullable, isNullType, isNumber, isNumberObjectType, isNumberValueType, isObjectType, isOnlyBigInt, isOrdinaryFunction, isRawTypeOfTemplatizedType, isReadonlyArrayType, isRecordType, isRegexpType, isResolved, isSomeUnknownType, isString, isStringObjectType, isStringValueType, isStruct, isStructuralInterface, isSubtype, isSubtype, isSubtypeOf, isSubtypeOf, isSubtypeWithoutStructuralTyping, isSuccessfullyResolved, isSymbol, isSymbolObjectType, isSymbolValueType, isTemplateType, isTemplatizedType, isUnionType, isUnsuccessfullyResolved, isVoidable, isVoidType, loosenTypecheckingDueToForwardReferencedSupertype, matchConstraint, matchesNumberContext, matchesObjectContext, matchesStringContext, matchesSymbolContext, mergeSupertypeTemplateTypes, resolve, restrictByNotNull, restrictByNotNullOrUndefined, restrictByNotUndefined, setValidator, toAnnotationString, toMaybeEnumElementType, toMaybeEnumType, toMaybeFunctionType, toMaybeFunctionType, toMaybeNamedType, toMaybeObjectType, toMaybeRecordType, toMaybeTemplateType, toMaybeTemplatizedType, toMaybeUnionType, toObjectType, toString
-
Method Details
-
getSlot
Default getSlot implementation. This gets overridden by FunctionType for lazily-resolved prototypes. -
getOwnSlot
-
getTypeOfThis
-
getTemplateTypes
Gets the declared default element type.- See Also:
-
getJSDocInfo
Gets the docInfo for this type.- Overrides:
getJSDocInfoin classJSType
-
setJSDocInfo
-
getReferenceName
Gets the reference name for this object. This includes named types like constructors, prototypes, and enums. It notably does not include literal types like strings and booleans and structural types.Returning an empty string means something different than returning null. An empty string may indicate an anonymous constructor, which we treat differently than a literal type without a reference name. e.g. in
InstanceObjectType.appendTo(TypeStringBuilder)- Returns:
- the object's name or
nullif this is an anonymous object
-
hasReferenceName
public final boolean hasReferenceName()INVARIANT:hasReferenceName()is true if and only ifgetReferenceName()returns a non-null string.- Returns:
- true if the object is named, false if it is anonymous
-
getNormalizedReferenceName
Due to the complexity of some of our internal type systems, sometimes we have different types constructed by the same constructor. In other parts of the type system, these are called delegates. We construct these types by appending suffixes to the constructor name.The normalized reference name does not have these suffixes, and as such, recollapses these implicit types back to their real type. Note that suffixes such as ".prototype" can be added after the delegate suffix, so anything after the parentheses must still be retained.
-
getDisplayName
Description copied from class:JSTypeReturns 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.- Overrides:
getDisplayNamein classJSType- Returns:
- the display name of the type, or null if one is not available
-
createDelegateSuffix
Creates a suffix for a proxy delegate.- See Also:
-
getRawType
-
testForEquality
Description copied from class:JSTypeComparesthisandthat.- Overrides:
testForEqualityin classJSType- Returns:
Tri.TRUEif the comparison of values ofthistype andthatalways succeed (such asundefinedcompared tonull)Tri.FALSEif the comparison of values ofthistype andthatalways fails (such asundefinedcompared tonumber)Tri.UNKNOWNif the comparison can succeed or fail depending on the concrete values
-
getConstructor
Gets this object's constructor.- Returns:
- this object's constructor or
nullif it is a native object (constructed natively v.s. by instantiation of a function)
-
getSuperClassConstructor
-
getClosestDefiningType
Returns the closest ancestor that defines the property including this type itself. -
findClosestDefinition
Returns the closest definition of the property including this type itself. -
getImplicitPrototype
Gets the implicit prototype (a.k.a. the[[Prototype]]property). -
getImplicitPrototypeChain
Returns a lazy, dynamicIterablefor the types forming the implicit prototype chain of this type.The chain is iterated bottom to top; from the nearest ancestor to the most distant. Iteration stops when the next ancestor would be a
nullreference.The created
Iterators will not reflect changes to the prototype chain of elements it has already iterated past, but will reflect those of upcoming elements. Neither theIterablenor itsIteratorsupport mutation. -
defineDeclaredProperty
Defines a property whose type is explicitly declared by the programmer.- Parameters:
propertyName- the property's nametype- the typepropertyNode- the node corresponding to the declaration of property which might later be accessed usinggetPropertyNode.
-
defineSynthesizedProperty
Defines a property whose type is on a synthesized object. These objects don't actually exist in the user's program. They're just used for bookkeeping in the type system. -
defineInferredProperty
Defines a property whose type is inferred.- Parameters:
propertyName- the property's nametype- the typepropertyNode- the node corresponding to the inferred definition of property that might later be accessed usinggetPropertyNode.
-
getPropertyNode
Gets the node corresponding to the definition of the specified property. This could be the node corresponding to declaration of the property or the node corresponding to the first reference to this property, e.g., "this.propertyName" in a constructor. Note this is mainly intended to be an estimate of where in the source code a property is defined. Sometime the returned node is not even part of the global AST but in the AST of the JsDoc that defines a type.- Parameters:
propertyName- the name of the property- Returns:
- the
Nodecorresponding to the property or null.
-
getPropertyDefSite
-
getPropertyJSDocInfo
-
getOwnPropertyJSDocInfo
Gets the docInfo on the specified property on this type. This should not be implemented recursively, as you generally need to know exactly on which type in the prototype chain the JSDocInfo exists. -
getOwnPropertyDefSite
-
setPropertyJSDocInfo
Sets the docInfo for the specified property from theJSDocInfoon its definition.- Parameters:
info-JSDocInfofor the property definition. May benull.
-
setPropertyNode
Sets the node where the property was defined. -
findPropertyTypeWithoutConsideringTemplateTypes
Description copied from class:JSTypeLooks 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:
findPropertyTypeWithoutConsideringTemplateTypesin classJSType
-
getPropertyType
Gets the property type of the property whose name is given. If the underlying object does not have this property, the Unknown type is returned to indicate that no information is available on this property. This gets overridden by FunctionType for lazily-resolved call() and bind() functions.- Returns:
- the property's type or
UnknownType. This method never returnsnull.
-
getPropertyKind
Description copied from class:JSTypeChecks whether the property is present on the object.- Overrides:
getPropertyKindin classJSType- Parameters:
propertyName- The property name.autobox- Whether to check for the presents on an autoboxed type
-
getOwnPropertyKind
Checks whether the property whose name is given is present directly on the object. Returns false even if it is declared on a supertype. -
hasOwnProperty
Checks whether the property whose name is given is present directly on the object. Returns false even if it is declared on a supertype. -
getOwnPropertyNames
Returns the names of all the properties directly on this type. Overridden by FunctionType to add "prototype". -
isPropertyTypeInferred
Checks whether the property's type is inferred. -
isPropertyTypeDeclared
Checks whether the property's type is declared. -
isStructuralType
public boolean isStructuralType()- Overrides:
isStructuralTypein classJSType
-
hasOwnDeclaredProperty
Whether the given property is declared on this object. -
isPropertyInExterns
Checks whether the property was defined in the externs. -
getPropertiesCount
public final int getPropertiesCount()Gets the number of properties of this object. -
getPropertyNames
Returns a list of properties defined or inferred on this type and any of its supertypes. -
visit
Description copied from class:JSTypeVisit this type with the given visitor. -
getPossibleToBooleanOutcomes
Description copied from class:JSTypeComputes 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} - Specified by:
getPossibleToBooleanOutcomesin classJSType- Returns:
- the set of boolean literals for this type
-
isUnknownType
public boolean isUnknownType()We treat this as the unknown type if any of its implicit prototype properties is unknown.- Overrides:
isUnknownTypein classJSType
-
isObject
public boolean isObject()Description copied from class:JSTypeTests whether this type is anObject, or any subtype thereof. -
hasCachedValues
public boolean hasCachedValues()Returns true if any cached values have been set for this type. If true, then the prototype chain should not be changed, as it might invalidate the cached values. -
clearCachedValues
public void clearCachedValues()Clear cached values. Should be called before making changes to a prototype that may have been changed since creation. -
isNativeObjectType
public boolean isNativeObjectType()Whether this is a built-in object.- Overrides:
isNativeObjectTypein classJSType
-
cast
A null-safe version of JSType#toObjectType. -
isFunctionPrototypeType
public final boolean isFunctionPrototypeType()Description copied from class:JSTypeWhether this is the prototype of a function.- Overrides:
isFunctionPrototypeTypein classJSType
-
getOwnerFunction
-
getCtorImplementedInterfaces
Gets the interfaces implemented by the ctor associated with this type. Intended to be overridden by subclasses. -
getCtorExtendedInterfaces
Gets the interfaces extended by the interface associated with this type. Intended to be overridden by subclasses. -
getPropertyTypeMap
get the map of properties to types covered in an object type- Returns:
- a Map that maps the property's name to the property's type
-
getEnumeratedTypeOfEnumObject
-