標籤:typescript
1.1 Ambient Declarations
1.1 環境聲明
An ambient declaration introduces a variable into a TypeScript scope, but has zero impact on the emitted JavaScript program. Programmers can use ambient declarations to tell the TypeScript compiler that some other component will supply a variable. For example, by default the TypeScript compiler will print an error for uses of undefined variables. To add some of the common variables defined by browsers, a TypeScript programmer can use ambient declarations. The following example declares the ‘document‘ object supplied by browsers. Because the declaration does not specify a type, the type ‘any‘ is inferred. The type ‘any‘ means that a tool can assume nothing about the shape or behavior of the document object. Some of the examples below will illustrate how programmers can use types to further characterize the expected behavior of an object.
環境聲明介紹一個變數在TypeScript的範圍,但在JavaScript程式裡毫無影響。程式員可以使用環境聲明去告訴TypeScript編譯器,組件將支援這個變數。例如,預設的TypeScript編譯器會給報一個使用未定義變數的錯誤。添加一些平常瀏覽器定義的變數。TypeScript程式員可以使用環境聲明。接下來的例子聲明瀏覽器提供的“document”對象。因為這個聲明沒有指定類型,類型被推斷為any。“any”意味著工具可以假設document對象的形狀或行為。下面的一些例子會說明程式員如何能夠使用類型進一步描述對象的預期行為。
declare var document; document.title = "Hello";// Ok because document has been declared
In the case of ‘document‘, the TypeScript compiler automatically supplies a declaration, because TypeScript by default includes a file ‘lib.d.ts‘ that provides interface declarations for the built-in JavaScript library as well as the Document Object Model.
在document這個例子中,TypeScript編譯器自動地支援一個聲明。因為TypeScri預設包含的lib.d.ts檔案裡提供了聲明介面 為構建JavaScript庫以及文件物件模型。
The TypeScript compiler does not include by default an interface for jQuery, so to use jQuery, a programmer could supply a declaration such as:
TypeScript編譯器並不包含預設的JQuery介面,因此要用JQuery。使用者需要像這樣聲明:
declare var $;
Section 1.3 provides a more extensive example of how a programmer can add type information for jQuery and other libraries.
1.3小節提供更多的程式員如何添加JQuery類型資訊擴充例子和其他庫。
1.2 Function Types 函數類型
Function expressions are a powerful feature of JavaScript. They enable function definitions to create closures: functions that capture information from the lexical scope surrounding the function‘s definition. Closures are currently JavaScript‘s only way of enforcing data encapsulation. By capturing and using environment variables, a closure can retain information that cannot be accessed from outside the closure. JavaScript programmers often use closures to express event handlers and other asynchronous callbacks, in which another software component, such as the DOM, will call back into JavaScript through a handler function.
函數運算式是JavaScript的一個強大的功能。他們啟用函數定義來建立閉包:從功能的定義的詞法範圍捕獲資訊的函數。閉包是目前JavaScript的執行方式的資料封裝。通過捕獲和使用環境變數,一個閉包可以保留不能從外部存取的資訊。JavaScript程式員經常使用閉包來表達事件處理常式和其他非同步回調,其中另一個軟體組件,如DOM,會被JavaScript通過函數控制代碼調用。
TypeScript function types make it possible for programmers to express the expected signature of a function. A function signature is a sequence of parameter types plus a return type. The following example uses function types to express the callback signature requirements of an asynchronous voting mechanism.
TypeScript函數類型可以表達程式員期望的函數簽名,一個函數簽名是一個參數類型的數列和傳回型別,接下來的例子用到了函數類型去表達一個引用了非同步投票機制的回調簽名。
function vote(candidate: string, callback: (result: string) => any) { // ...}
TypeScript官方文檔翻譯-5