1. Compare JavaScript
- Typescript is a superset of JavaScript, can be compiled into JavaScript, mainly provide the type system and other enhanced code readability and maintainability, suitable for large-scale projects, multi-person collaboration;
- Typescript will only perform static checks, if found errors, compile the time will be error;
2. Original data type
- Boolean,number,string,null,undefined,void (typically used for return values);
- The difference from void is that undefined and null are subtypes of all types, that is, variables of type undefined, which can be assigned to variables of type number, void;
3. Any value any
- An assignment is any type, or any value type if no type is specified;
4. Type inference
- If the variable type assignment is not specified, it is inferred from the type of the previous variable, and then the re-assignment detects the type;
5. Union type (Union Types)
- Various types of Let A:string | Number
6. Interface (interface)
- Used to define the type of the object, abstract the behavior of the class, and describe the object; The first letter of the interface is generally uppercase;
- Optional attribute: A?: string;
- Any attribute: [propname:string]: string, once any attribute is defined, the property and optional attribute must be its child properties;
- Read-only property: ReadOnly a:string; can only be assigned at the creation stage;
7. Arrays
- Type +[] means, let arr:number[] = [1, 2, 3];
- Array generic representation, let arr:array<number> = [1, 2, 3];
- Interface representation
Interface Numberarray {
[Index:number]: Number
}
Let Arr:numberarray = [1, 2, 3];
- Any indicates that any type is allowed in the array;
- The common array of classes has its own interface definitions, such as,
IArguments
NodeList
, HTMLCollection
etc.;
8. Functions
- The parameters are constrained and no extra (or less) parameters are allowed to be entered.
- In the TypeScript type definition, it
=>
is used to denote the definition of a function, the left is the input type, it needs to be enclosed in parentheses, and the right is the output type;
- The required parameters are not allowed to appear after the optional parameters;
- TypeScript will recognize the parameter that added the default value as an optional parameter, which is not subject to the "optional parameter must be followed by the required parameters" limit;
- Overloading allows a function to accept different quantities or types of parameters when different processing is made;
9. Type assertion (type assertion)
- Can be used to manually specify the type of a value;
- < types > variables;
- Type assertion is not a type conversion, and it is not permissible to assert a type that does not exist in a union type;
10. Declaration File
- DECLARE var jQuery: (selector:string) = any;
- Usually we put the type declaration in a separate file, this is the declaration file, the contract with. D.ts as the suffix;
- Then, at the beginning of the file used, a "three slash instruction" is used to refer to the declaration file.
<reference path= './jquery.d.ts '/>
11. Built-in objects
- Boolean, Error, Date, regexp, etc.;
- Built-in objects for DOM and BOM;
- The definition file for the TypeScript core Library defines the types required for all browser environments, and is preset in TypeScript;
- The TypeScript core library definition does not include the node. JS section
12. Advanced
TypeScript Xiao Kee