Typescript # 2 type Overview

Primitive types


Javascript primitive types also apply to TypeScript type system. Thus, string, number, boolean type annotations may also be used:

 let num: number;
 let str: string;
 let bool: boolean;

 num = 123;
 num = 123.45;
 num = '123'; //Type '"123"' is not assignable to type 'number'

 str = '123';
 str = 123; //Type '123' is not assignable to type 'string'

 bool = true;
 bool = false;
 bool = 'false';//Type '"false"' is not assignable to type 'boolean'.

Array

Type of annotation + [], which allows you to safely use any operation related to the array, and it can place the behavior of some similar type of error to assign members.

// 元素类型加[]写法
let list:number[] = [1,2,3]
// 数组泛型写法
let list: Array<number> = [1,2,3]

Originator

Known elements the number and type of arrays

// 声明一个元组类型
let x: [string, number];
// 初始化赋值
x = ['hello', 10]; // OK
// 初始化错误
x = [10, 'hello']; // Error

enumerate

Usually in the computer language used to represent a particular state value, such as not intuitive and readability is poor. Enumeration is to give some of the state of the computer (digital) and a corresponding natural language word meaning association, which aims to enumerate all cases with written, this corresponds to a different enumeration values ​​depending on the state, raising the readability of the code.

Enumeration type can be a set of values ​​given friendly names. By default, the element number from 0 to, you can also manually specify the number of members.

enum Color {Red = 1,Geeen = 2, Blue = 4}
let c: Color = Color.Green

Compiled

var Color;
(function (Color) {
    Color[Color["Red"] = 1] = "Red";
    Color[Color["Green"] = 2] = "Green";
    Color[Color["Blue"] = 4] = "Blue";
})(Color || (Color = {}));
console.log(Color) // 在函数最后进行打印,返回一个JSON
var c = Color.Green;

The name of the corresponding enumeration is a variable, and then determine whether there is value, if no assignment json, and then call the function from where an object is assigned, you can finally print function, this time in the json values ​​are all expressions returns a value, its return value is assigned to the right of the equal sign.

{ '1': 'Red', '2': 'Green', '3': 'Blue', Red: 1, Green: 2, Blue: 3 }

If you can not quite understand the code above, then you must know the console.log(Color["Red"]=0)return 0.

In addition, the enumerated type provides a traversal is that you can be worth by the enumeration to its name (can be seen from the source code compiled above). For example, we know the value of 2, but not sure it is mapped to the Color in which the name, we can find the appropriate name:

enum Color {Red = 1, Green, Blue}
let colorName: string = Color[2];

console.log(colorName);  // 显示'Green'因为上面代码里它的值是2

null & undefined

null and undefined are all other types of subtype

const n1:null = 123;
const n2:undefined = '123';

If the value of a variable does require a null or undefined, like the following can be so used, ts automatically infer the correct type based on if / else:

// 这是"联合类型", 在"高级类型"中会有详细介绍, 表示n可能是undefined也可能是number
let num: undefined|number;

if(Math.random()>0.5) num = 1;

if(undefined !== num) {
    num++;
}

any

any type occupies a special position in TypeScript type system. It gives you a type system [backdoor], TypeScript will turn off the type check. any type compatible with all types of systems (including itself). Therefore, all types can be assigned to it. It can also be assigned to any other type.

void

Used :voidto represent a function has no return value

 function log(message:string):void{
    console.log(message)
}

Declare a variable of type void nothing Dayong, because you're only given undefined and null for it:

let unusable:void = undefined

never

I never said they did not reach, really hard to describe in words, mainly used in the case of the throw:

function error():never{
    throw '错了!';
}

Guess you like

Origin www.cnblogs.com/CharmanderS5/p/11129850.html