10.4 Export Declarations

An export declaration declares an externally accessible namespace member. An export declaration is simply a regular declaration prefixed with the keyword export.

The members of a namespace’s export declaration space (section 2.3) constitute the namespace’s export member set. A namespace’s instance type is an object type with a property for each member in the namespace’s export member set that denotes a value.

An exported member depends on a (possibly empty) set of named types (section 3.7). Those named types must be at least as accessible as the exported member, or otherwise an error occurs.

The named types upon which a member depends are the named types occurring in the transitive closure of the directly depends on relationship defined as follows:

  • A variable directly depends on the Type specified in its type annotation.
  • A function directly depends on each Type specified in a parameter or return type annotation.
  • A class directly depends on each Type specified as a type parameter constraint, each TypeReference specified as a base class or implemented interface, and each Type specified in a constructor parameter type annotation, public member variable type annotation, public member function parameter or return type annotation, public member accessor parameter or return type annotation, or index signature type annotation.
  • An interface directly depends on each Type specified as a type parameter constraint, each TypeReference specified as a base interface, and the ObjectType specified as its body.
  • A namespace directly depends on its exported members.
  • A Type or ObjectType directly depends on every TypeReference that occurs within the type at any level of nesting.
  • A TypeReference directly depends on the type it references and on each Type specified as a type argument.

A named type T having a root namespace R (section 2.3) is said to be at least as accessible as a member M if

  • R is the global namespace or a module, or
  • R is a namespace in the parent namespace chain of M.

In the example

  1. interface A { x: string; }
  2. namespace M {
  3. export interface B { x: A; }
  4. export interface C { x: B; }
  5. export function foo(c: C) { }
  6. }

the ‘foo’ function depends upon the named types ‘A’, ‘B’, and ‘C’. In order to export ‘foo’ it is necessary to also export ‘B’ and ‘C’ as they otherwise would not be at least as accessible as ‘foo’. The ‘A’ interface is already at least as accessible as ‘foo’ because I t is declared in a parent namespace of foo’s namespace.