TypeScript learning notes, typescript notes

Source: Internet
Author: User

TypeScript learning notes, typescript notes

Comparison with JS

Advantages:

1. ts is a js superset that is compatible with js and can load and run JS Code.

2. Add comments so that the compiler can understand the supported objects and functions. The compiler will remove the comments without increasing the overhead.

3. provide static type check during compilation through type annotation.

4. The biggest feature of ts is typing, which is called TypeScript. Compared with weak js types, typed typescripts are easier to maintain.

 

Syntax features:

1. Declare the variable type

Use the var keyword variable name + colon + data type to specify
var name: string = "bob";
Var list: number [] = [1, 2, 3]; var name: string [] = ["a dragon", "A cat", "a dog"];
Var arr: Array = [1, 2, 3, "a", "B", "c"]; // any type of Array
var list: any[] = [1, true, "free"];

2. enum

Enum Color {Red, // list of enumerated elements Green, Blue}; var c: Color = Color. Green;

3. Functions

Optional parameter: After the parameter name, add a question mark before the colon to indicate that this parameter is optional. Function buildName (firstName: string, lastName? : String) {}// lastName is an optional parameter
Default parameter: A value is directly given after the parameter name. If the value is not passed in, the value is assigned as the default value. Function buildName (firstName: string, lastName = "Smith "){}

4. Class

Class name {name: string; // defines the attributes of the class fun () {// defines a method without a returned value // defines the functions to be implemented by this method} say (): string {// define a method whose return value type is string // define the function to be implemented by this method return "return Value"; // return the function value with the return keyword }}

5. Interface

In TypeScript, interfaces are used as constraints. When compiled into JavaScript, all interfaces are erased because JavaScript does not have interfaces. In TypeScript, the interface is declared with the keyword interface.
Interface LabelledValue {// defines the interface label: string; // defines attributes}
// In TypeScript, we can use interfaces to constrain the method signature. Interface SearchFunc {(source: string, subString: string): boolean; // defines an anonymous method} var mySearch: SearchFunc; mySearch = function (source: string, subString: string) {// implement interface var result = source. search (subString); // call the system method search to find the string location if (result =-1) {return false;} else {return true ;}}
// Constraint array type interface StringArray {// define array interface [index: number]: string; // type of each array element} var myArray: StringArray; myArray = ["Bob", "Fred"]; // constraint class type interface IPrint {print ();} class A implements IPrint {// implement interface print () {// implement the method document in the interface. write ("implemented interface") ;}} var B = new A (); B. print ();
// The interface inherits interface Shape {color: string;} interface PenStroke {penWidth: number;} interface Square extends Shape, PenStroke {sideLength: number;} // note that, although multiple interfaces can be inherited, compilation fails if the types of attributes with the same name are different in the inherited interfaces. Code: interface Shape {color: string; test: number;} interface PenStroke extends Shape {penWidth: number; test: string ;}

Note:

1. To Use ts, you must compile it first. The compiling result is to generate a js file. You can use the tsc command of the TypeScript compiler to complete this process.

2: do not regard TypeScript as a new language. It is just a tool to improve the quality of JavaScript code. In the end, TypeScript will still be compiled into JavaScript.

Online compilation link: http://www.typescriptlang.org/play/index.html

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.