TS: typescript type annotation
- In ts, type annotation is a way to add constraints to functions or variables.
1. Basic type
- boolean,number,string
2. Array
-
ts can manipulate array elements like js, and there are two ways to define numbers.
let list : number[] = [1,2,3]; //or let list : Array<number> = [1,2,3];
3. Tuples
-
The tuple type indicates that the types in the array are allowed to be different, but the assignment needs to be assigned in the order of the types.
let list : [string , number]; x = ['hello', 24]; // true x = [20 , 'hello']; // false
When accessing elements in a tuple, also pay attention to the order of types:
console.log(x[0].substr(1)); //true console.log(x[1].substr(1)); //false,number类型没有这个方法
When accessing a cross-boundary element, the union type is used instead, but you cannot try to assign values other than the existing type:
x[3] = 'world'; // ok,string is allowed x[3] = true; // false,boolean不在这个元组内
4. Enumeration
-
The enum type is a supplement to js. Enumeration can be used to give a name to a group of values. Generally, an enumeration starts from 0 and assigns values. You can manually assign values from other numbers, or you can assign all values manually.
enum Color { red,green,blue}; // 从0开始 let r : Color = Color.red; enum Color { red = 1,green,blue}; // 从1开始 enum Color { red = 1,green = 4,blue = 2};
There is also a traversal of the enumeration, and the corresponding name can be found by the value of the enumeration.
enum Color { red,green,blue}; let colorname : string = Color[0]; console.log(colorname); // 'red'
5. Any
-
Any can be used to mark some variables that have not yet been determined.
let notsure : any[] = [1,'hello',true];
The difference with Object: Variables of Object type only allow you to assign values to them, but you cannot call methods of the corresponding type.
let notsure : any = 1; notsure.toFixed(); //ok let notsure : Object = 1; notsure.toFixed(); //error,property 'toFixed' does not exist.
6. void
-
Void is the opposite of any, which means that there is no type. It is often used in functions that return void.
function warning() : void { console.log('warning!'); }
7. null和undefined
- By default,
null
sumundefined
is a subtype of all types. That you cannull
andundefined
assigned tonumber
a variable of type.
8. never
-
What values will never exist, for example, represents a
never
variable might be; type is a function of the type that always throws an exception or no return valuenever
type, when they are never astrue
when the type of protection apply.never
A type is a subtype of any type and can be assigned to any type; however, no type isnever
a subtype or can be assigned to anever
type (except fornever
itself). Even ifany
neither can be assigned tonever
.// 返回never的函数必须存在无法达到的终点 function error(message: string): never { throw new Error(message); } // 推断的返回值类型为never function fail() { return error("Something failed"); } // 返回never的函数必须存在无法达到的终点 function infiniteLoop(): never { while (true) { } }
9. Object type
-
The object type represents a non-primitive type, which is a type other than the above types. Using this type can better represent an API such as Object.create.
declare function create(o: object | null): void; create({ prop: 0 }); // OK create(null); // OK create(42); // Error create("string"); // Error create(false); // Error create(undefined); // Error
10. Type Assertion
-
Type assertion is similar to type conversion, but without special data checking and deconstruction.
let someValue: any = "this is a string"; let strLength: number = (<string>someValue).length; // or let someValue: any = "this is a string"; let strLength: number = (someValue as string).length;