TypeScript---数据类型

TypeScript---数据类型

 

 

 

 

 

//字符串
let str: string = "你好ts"
let str1: string = "你好typescript"

//布尔
var flag: boolean = true
flag = false
//数字
var a: number = 123
console.log(a);
a = 12.41
console.log(a);
//字符串
let str2: string = "this is ts"
str2 = "哈哈哈"
console.log(str2);
//数字
let arr: number[] = [1234, 435]
console.log(arr);
//字符串
let arr1: string[] = ["psj", "sjh"]
console.log(arr1);
//数组写法1
let arr3: Array<number> = [12, 34, 42]
console.log(arr3);
//数组写法2
let arr4: Array<string> = ["string", "es6"]
console.log(arr4);
//元组类型(tuple) 数组的一种
let arr5: [string, number, boolean] = ["ts", 3.23, true]
console.log(arr5);
//枚举类型(enum)

enum Flag { success = 1, error = -1 }
var f: Flag = Flag.success
console.log(f);
console.log(Flag.error);

enum Color { red, blue = 10, yellow };
var c: Color = Color.blue;
console.log(c); // 10
var c1: Color = Color.yellow;
console.log(c1); //11

 

//null 空
var num4: number | null | undefined;
num4 = 1234;
console.log(num4);

//void类型:TS中void表示没有任何类型,一般用于定义方法的时候,方法没有返回值
//es5
// function run() {
//     console.log('run');
// };
// run();

function run(): void {
    console.log('run'); //run
}
run(); //没有返回任何类型

function run2(): number {
    return 123
};
run();

//never类型
var aa: never;
aa = (() => {
    throw new Error("错误");
})()

 

posted @ 2020-04-29 10:45  jane_panyiyun  阅读(252)  评论(0编辑  收藏  举报