ts的九大数据类型,记录一下

2020-06-11 16:14:08 浏览数 (1)

代码语言:javascript复制
// 1.boolean
let isTrue: boolean = false;
console.log(isTrue);
 
// 2.string
let str: string = "jianan";
console.log(str);
 
// 3.number
let age: number = 26;
console.log(age);
 
// 4.数组
let arr: number[] = [1, 2, 3];
 
let arr2: Array<string> = ["1", "2", "3", "4"];
console.log(arr[0], arr2[0]);
 
// 5.元组
let tup: [number, string] = [27, "jianan"];
console.log(tup);
console.log(tup[0], tup[1]);
 
let a: Array<[number, string]> = [[27, "jianan"], [28, "xixi"]];
console.log(a);
 
// 6.enum
enum color {
    red = 2,
    green,
    yellow
}
 
let c = color.green;
console.log(c);
 
// 7.any
let notsure: any = {
    a: 0,
    b: 2
};
 
// 8.联合
let unionNum: string | number;
unionNum = "jianan";
unionNum = 777;
// unionNum = true;
 
//-----不确定是哪个值时,只能访问联合属性的共有方法
function getLen(someThing: string | number): string  {
    return someThing.toString();
 
// 9.void类型
function test(n: number): void {
    console.log(11);
}

0 人点赞