Class NamedType


  • public final class NamedType
    extends ProxyObjectType
    A NamedType is a named reference to some other type. This provides a convenient mechanism for implementing forward references to types; a NamedType can be used as a placeholder until its reference is resolved. It is also useful for representing type names in JsDoc type annotations, some of which may never be resolved (as they may refer to types in host systems not yet supported by JSCompiler, such as the JVM.)

    An important distinction: NamedType is a type name reference, whereas ObjectType is a named type object, such as an Enum name. The Enum itself is typically used only in a dot operator to name one of its constants, or in a declaration, where its name will appear in a NamedType.

    A NamedType is not currently a full-fledged typedef, because it cannot resolve to any JavaScript type. It can only resolve to a named JSTypeRegistry type, or to FunctionType or EnumType.

    If full typedefs are to be supported, then each method on each type class needs to be reviewed to make sure that everything works correctly through typedefs. Alternatively, we would need to walk through the parse tree and unroll each reference to a NamedType to its resolved type before applying the rest of the analysis.

    TODO(user): Revisit all of this logic.

    The existing typing logic is hacky. Unresolved types should get processed in a more consistent way, but with the Rhino merge coming, there will be much that has to be changed.

    • Method Detail

      • getReferencedType

        public JSType getReferencedType()
        Returns the type to which this refers (which is unknown if unresolved).
      • getReferenceName

        public java.lang.String getReferenceName()
        Description copied from class: ObjectType
        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)

        Overrides:
        getReferenceName in class ProxyObjectType
        Returns:
        the object's name or null if this is an anonymous object
      • isNominalType

        public boolean isNominalType()
        Description copied from class: JSType
        Whether this type is a nominal type (a named instance object or a named enum).
        Overrides:
        isNominalType in class ProxyObjectType
      • setValidator

        public boolean setValidator​(com.google.common.base.Predicate<JSType> validator)
        Description copied from class: JSType
        Certain types have constraints on them at resolution-time. For example, a type in an @extends annotation must be an object. Clients should inject a validator that emits a warning if the type does not validate, and return false.
        Overrides:
        setValidator in class ProxyObjectType
      • isObject

        public boolean isObject()
        Description copied from class: JSType
        Tests whether this type is an Object, or any subtype thereof.
        Overrides:
        isObject in class ObjectType
        Returns:
        this <: Object
      • visit

        public <T> T visit​(Visitor<T> visitor)
        Description copied from class: JSType
        Visit this type with the given visitor.
        Overrides:
        visit in class ProxyObjectType
        Returns:
        the value returned by the visitor
        See Also:
        Visitor