12.1 Ambient Declarations

Ambient declarations are written using the declare keyword and can declare variables, functions, classes, enums, namespaces, or modules.

  AmbientDeclaration:   declareAmbientVariableDeclaration   declareAmbientFunctionDeclaration   declareAmbientClassDeclaration   declareAmbientEnumDeclaration   declareAmbientNamespaceDeclaration

12.1.1 Ambient Variable Declarations

An ambient variable declaration introduces a variable in the containing declaration space.

  AmbientVariableDeclaration:   varAmbientBindingList;   letAmbientBindingList;   constAmbientBindingList;

  AmbientBindingList:   AmbientBinding   AmbientBindingList,AmbientBinding

  AmbientBinding:   BindingIdentifierTypeAnnotationopt

An ambient variable declaration may optionally include a type annotation. If no type annotation is present, the variable is assumed to have type Any.

An ambient variable declaration does not permit an initializer expression to be present.

12.1.2 Ambient Function Declarations

An ambient function declaration introduces a function in the containing declaration space.

  AmbientFunctionDeclaration:   functionBindingIdentifierCallSignature;

Ambient functions may be overloaded by specifying multiple ambient function declarations with the same name, but it is an error to declare multiple overloads that are considered identical (section 3.11.2) or differ only in their return types.

Ambient function declarations cannot specify a function bodies and do not permit default parameter values.

12.1.3 Ambient Class Declarations

An ambient class declaration declares a class type and a constructor function in the containing declaration space.

  AmbientClassDeclaration:   classBindingIdentifierTypeParametersoptClassHeritage{AmbientClassBody}

  AmbientClassBody:   AmbientClassBodyElementsopt

  AmbientClassBodyElements:   AmbientClassBodyElement   AmbientClassBodyElementsAmbientClassBodyElement

  AmbientClassBodyElement:   AmbientConstructorDeclaration   AmbientPropertyMemberDeclaration   IndexSignature

  AmbientConstructorDeclaration:   constructor(ParameterListopt);

  AmbientPropertyMemberDeclaration:   AccessibilityModifieroptstaticoptPropertyNameTypeAnnotationopt;   AccessibilityModifieroptstaticoptPropertyNameCallSignature;

12.1.4 Ambient Enum Declarations

An ambient enum is grammatically equivalent to a non-ambient enum declaration.

  AmbientEnumDeclaration:   EnumDeclaration

Ambient enum declarations differ from non-ambient enum declarations in two ways:

  • In ambient enum declarations, all values specified in enum member declarations must be classified as constant enum expressions.
  • In ambient enum declarations that specify no const modifier, enum member declarations that omit a value are considered computed members (as opposed to having auto-incremented values assigned).

Ambient enum declarations are otherwise processed in the same manner as non-ambient enum declarations.

12.1.5 Ambient Namespace Declarations

An ambient namespace declaration declares a namespace.

  AmbientNamespaceDeclaration:   namespaceIdentifierPath{AmbientNamespaceBody}

  AmbientNamespaceBody:   AmbientNamespaceElementsopt

  AmbientNamespaceElements:   AmbientNamespaceElement   AmbientNamespaceElementsAmbientNamespaceElement

  AmbientNamespaceElement:   exportoptAmbientVariableDeclaration   exportoptAmbientLexicalDeclaration   exportoptAmbientFunctionDeclaration   exportoptAmbientClassDeclaration   exportoptInterfaceDeclaration   exportoptAmbientEnumDeclaration   exportoptAmbientNamespaceDeclaration   exportoptImportAliasDeclaration

Except for ImportAliasDeclarations, AmbientNamespaceElements always declare exported entities regardless of whether they include the optional export modifier.