Block-level scope variables:
1, not being read or written statement before
the console.log (NUM); the let NUM: Number = 0; // given
2, can still have a variable declaration before the block-level scope to capture it through the function, but can not go calling this function in the variable declaration
function Test () { return num; } Test (); // can not call is declared before num Test the let num: Number = 0;
3, block-level scope variables of the same name to be in different blocks declared in
Note: var variable declaration has function scope, there is a statement in advance , so no matter the statement several times, will get the same variable
interface:
1, the interface keyword interface to define
2, TypeScript, the role of these interfaces is named type and your code or third-party code defined contract and make code look better understanding
3, type checker not to check the object implements this interface attributes in order , as long as the corresponding attribute is present and the type of a corresponding identical to
4, optional attributes: It is not necessary to attribute declaration form attrName ?: type
{obj interface width : Number;? // an optional attribute HEIGH: Number; // a common attribute
Readonly Shape: String; // read-only property
}
Note: The optional attribute can be possible to attribute pre-defined , can capture cited nonexistent property at the time of the error
5, read-only attribute: attribute name used before readonly designated read-only attribute, only the value is modified when the object is newly created, the array having unmodifiable typescript ReadonlyArray <T> Type
let roArr: Readonly<number> = [1, 2, 3]; let arr_one: Array<number> = roArr; // 报错 let arr_two: Array<number> = roArr as Array<number>; // 编译通过
Note: The ReadonlyArray not assigned to a regular array, but you can override the type of assertion
6, when for the attribute when should Readonly , for the variable time should const
Function type:
1, the interface can be described in a wide variety of configurations JavaScript objects have, in addition to describe a common object with attributes, but it can also be a function of the type described
2, need to interface defines a call signature , just like a list of parameters and return value type of functions "== Each parameter requires the name and type
interface SearchFunc { // 调用签名 (source: string, subString: string): boolean; } let mySearch: SearchFunc; mySearch = function (source: string, subString: string) { return source.search(subString) > -1; } let str: string = "Hello World"; alert(mySearch(str, "world")); // 弹出 false
Note: The parameter name of the function does not need to match the name defined in the interface, parameters one by one to be checked ,
Parameters corresponding to a position required type is compatible , if not specified type, typescript will deduce the parameters of Type
Indexable type:
1, similar to the type and function, but with an index signature , which describes the object type of the index , as well as the respective type returned index
2, TypeScript support string and numeric index neither index signature, which can use two types of indexes, the numerical index value must be a return string index return type subtype
interface StringArray { // 索引签名 [index: number]: string; } let myArray: StringArray; myArray = ["Lemon", "Yam"]; alert(myArray[0]);
NOTE: When simultaneously using two index signature, digital is converted into a string index Index
Class type:
1, use it to clear a class forced to comply with a contractual
2, has two types of class: static part type and of the Examples section type
3, when a class that implements the interface, only examples of its type checking portion
4, when a class inherits the interface type, it inherits the members of the class, but does not include its implementation , the interface will also inherit the class private and protected members
Note: private and protected members can only be achieved in this class or subclass