10.3 Import Alias Declarations

Import alias declarations are used to create local aliases for entities in other namespaces.

  ImportAliasDeclaration:   importBindingIdentifier=EntityName;

  EntityName:   NamespaceName   NamespaceName.IdentifierReference

An EntityName consisting of a single identifier is resolved as a NamespaceName and is thus required to reference a namespace. The resulting local alias references the given namespace and is itself classified as a namespace.

An EntityName consisting of more than one identifier is resolved as a NamespaceName followed by an identifier that names an exported entity in the given namespace. The resulting local alias has all the meanings of the referenced entity. (As many as three distinct meanings are possible for an entity name—value, type, and namespace.) In effect, it is as if the imported entity was declared locally with the local alias name.

In the example

  1. namespace A {
  2. export interface X { s: string }
  3. export var X: X;
  4. }
  5. namespace B {
  6. interface A { n: number }
  7. import Y = A; // Alias for namespace A
  8. import Z = A.X; // Alias for type and value A.X
  9. var v: Z = Z;
  10. }

within ‘B’, ‘Y’ is an alias only for namespace ‘A’ and not the local interface ‘A’, whereas ‘Z’ is an alias for all exported meanings of ‘A.X’, thus denoting both an interface type and a variable.

If the NamespaceName portion of an EntityName references an instantiated namespace, the NamespaceName is required to reference the namespace instance when evaluated as an expression. In the example

  1. namespace A {
  2. export interface X { s: string }
  3. }
  4. namespace B {
  5. var A = 1;
  6. import Y = A;
  7. }

‘Y’ is a local alias for the non-instantiated namespace ‘A’. If the declaration of ‘A’ is changed such that ‘A’ becomes an instantiated namespace, for example by including a variable declaration in ‘A’, the import statement in ‘B’ above would be an error because the expression ‘A’ doesn’t reference the namespace instance of namespace ‘A’.

When an import statement includes an export modifier, all meanings of the local alias are exported.