Typescript Official Document Translator-5

Source: Internet
Author: User
Tags call back


1.1 Ambient Declarations

1.1 Environmental Statement

An ambient declaration introduces a variable to a TypeScript scope, but have zero impact on the emitted JavaScript Progra M. programmers can use ambient declarations to tell the TypeScript compiler that some other component would supply a VARIAB Le. For example, by default the TypeScript compiler would 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 this a tool can assume nothing about the shape or behavior of the Document object. Some of the examples below would illustrate how programmers can use types to further characterize the expected behavior of An object.

The environment statement describes the scope of a variable in the typescript, but it has no effect in the JavaScript program. The programmer can use the environment Declaration to tell the Typescript compiler that the component will support this variable. For example, the default Typescript compiler will report an error that uses an undefined variable. Add some common browser-defined variables. Typescript programmers can use environment declarations. The following example declares the "document" object provided by the browser. Because this declaration does not specify a type, the type is inferred as any. "Any" means that the tool can assume the shape or behavior of the Document object. The following examples illustrate how programmers can use types to further describe the expected behavior of objects.

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 Inc Ludes a file ' lib.d.ts ' that provides interface declarations for the built-in JavaScript library as well as the Document O Bject Model.

In the document example, the typescript compiler automatically supports a declaration. Because the TYPESCRI default contains the Lib.d.ts file, the declaration interface is provided for building JavaScript libraries as well as the Document Object model.


The TypeScript compiler does not include by default an interface for jquery, so-to-use jquery, a programmer could supply a Declaration such as:

The typescript compiler does not include the default jquery interface, so use jquery. The user needs to declare like this:

declare var $;

Section 1.3 Provides a more extensive example of what a programmer can add type information for jQuery and other libraries.

Section 1.3 provides more programmers how to add jquery type information extension examples and other libraries.


1.2 Function Types Type


Function expressions is a powerful feature of JavaScript. They enable function definitions to create closures:functions this capture information from the lexical scope surrounding The function ' s definition. Closures is currently JavaScript's only to enforcing data encapsulation. By capturing and using environment variables, a closure can retain information that cannot being accessed from outside the CL Osure. JavaScript programmers often use closures to express event handlers and other asynchronous callbacks, in which another SOF Tware component, such as the DOM, would call back into JavaScript through a handler function.


Function expressions are a powerful feature of JavaScript. They enable function definitions to create closures: functions that capture information from the lexical scope defined by the function. Closures are data encapsulation of the current way JavaScript is executed. By capturing and using environment variables, a closure can retain information that cannot be accessed externally. JavaScript programmers often use closures to express event handlers and other asynchronous callbacks, where another software component, such as the DOM, is invoked by JavaScript through a function handle.


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 Mechani Sm.


The Typescript function type can express a programmer's expected function signature, a function signature is a sequence of parameter types and a return type, and the following example uses the function type to express a callback signature that refers to the asynchronous voting mechanism.


Function vote (Candidate:string, callback: (result:string) + any) {//...}


Typescript Official Document Translator-5

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.