TypeScript語言規範
12 Ambients
環境聲明用於在現有JavaScript代碼上提供靜態類型。 環境聲明與常規聲明的不同之處在於,不會爲它們生成JavaScript代碼。 環境聲明不是引入新的變量,函數,類,枚舉或命名空間,而是提供“存在”並通過外部方式(例如通過引用
12.1 Ambients聲明
環境聲明是使用define關鍵字編寫的,可以聲明變量,函數,類,枚舉,命名空間或模塊。
AmbientDeclaration:
declare AmbientVariableDeclaration
declare AmbientFunctionDeclaration
declare AmbientClassDeclaration
declare AmbientEnumDeclaration
declare AmbientNamespaceDeclaration
12.1.1 Ambient變量聲明
環境變量聲明在包含聲明空間中引入了一個變量。
AmbientVariableDeclaration:
var AmbientBindingList ;
let AmbientBindingList ;
const AmbientBindingList ;
AmbientBindingList:
AmbientBinding
AmbientBindingList , AmbientBinding
AmbientBinding:
BindingIdentifier TypeAnnotationopt
環境變量聲明可以選擇包含類型註釋。 如果不存在類型註釋,則假定變量的類型爲Any。
環境變量聲明不允許存在初始化程序表達式。
12.1.2 Ambient函數聲明
環境函數聲明在包含聲明空間中引入了一個函數。
AmbientFunctionDeclaration:
function BindingIdentifier CallSignature ;
可以通過指定多個具有相同名稱的環境函數聲明來重載環境函數,但是聲明多個被視爲相同(第3.11.2節)或僅其返回類型不同的重載是錯誤的。
環境函數聲明不能指定函數體,也不允許默認參數值。
12.1.3 Ambient類聲明
環境類聲明在包含的聲明空間中聲明類類型和構造函數。
AmbientClassDeclaration:
class BindingIdentifier TypeParametersopt ClassHeritage { AmbientClassBody }
AmbientClassBody:
AmbientClassBodyElementsopt
AmbientClassBodyElements:
AmbientClassBodyElement
AmbientClassBodyElements AmbientClassBodyElement
AmbientClassBodyElement:
AmbientConstructorDeclaration
AmbientPropertyMemberDeclaration
IndexSignature
AmbientConstructorDeclaration:
constructor ( ParameterListopt ) ;
AmbientPropertyMemberDeclaration:
AccessibilityModifieropt staticopt PropertyName TypeAnnotationopt ;
AccessibilityModifieropt staticopt PropertyName CallSignature ;
12.1.4 Ambient枚舉聲明
一個環境枚舉在語法上等效於一個非環境枚舉聲明。
AmbientEnumDeclaration:
EnumDeclaration
環境枚舉聲明與非環境枚舉聲明在兩個方面有所不同:
- 在環境枚舉聲明中,枚舉成員聲明中指定的所有值都必須歸類爲常量枚舉表達式。
- 在未指定const修飾符的環境枚舉聲明中,忽略值的枚舉成員聲明被視爲計算成員(與分配自動遞增的值相反)。
否則,以與非環境枚舉聲明相同的方式處理環境枚舉聲明。
12.1.5 Ambient命名空間聲明
環境命名空間聲明聲明一個命名空間。
AmbientNamespaceDeclaration:
namespace IdentifierPath { AmbientNamespaceBody }
AmbientNamespaceBody:
AmbientNamespaceElementsopt
AmbientNamespaceElements:
AmbientNamespaceElement
AmbientNamespaceElements AmbientNamespaceElement
AmbientNamespaceElement:
exportopt AmbientVariableDeclaration
exportopt AmbientLexicalDeclaration
exportopt AmbientFunctionDeclaration
exportopt AmbientClassDeclaration
exportopt InterfaceDeclaration
exportopt AmbientEnumDeclaration
exportopt AmbientNamespaceDeclaration
exportopt ImportAliasDeclaration
除了ImportAliasDeclarations之外,AmbientNamespaceElements始終聲明導出的實體,無論它們是否包括可選的export修飾符。
12.2 Ambient模塊聲明
AmbientModuleDeclaration聲明一個模塊。僅在有助於全局命名空間的源文件的頂層允許這種類型的聲明(第11.1節)。 StringLiteral必須指定頂級模塊名稱。不允許使用相對模塊名稱。
AmbientModuleDeclaration:
declare module StringLiteral { DeclarationModule }
AmbientModuleDeclaration中的ImportRequireDeclaration只能通過頂級模塊名稱引用其他模塊。不允許使用相對模塊名稱。
如果環境模塊聲明包括導出分配,則模塊內的任何聲明指定導出修飾符都是錯誤的。如果環境模塊聲明不包含導出分配,則將導出模塊中聲明的實體,無論其聲明是否包括可選的export修飾符。
環境模塊是“開放式”的,具有相同字符串文字名稱的環境模塊聲明構成單個模塊。例如,模塊“ io”的以下兩個聲明可能位於單獨的源文件中。
declare module "io" {
export function readFile(filename: string): string;
}
declare module "io" {
export function writeFile(filename: string, data: string): void;
}
這與單個組合聲明具有相同的效果:
declare module "io" {
export function readFile(filename: string): string;
export function writeFile(filename: string, data: string): void;
}