一、基礎(chǔ)類型:
boolean為布爾值類型这揣,如let flag:boolean = true;
number為數(shù)值類型匿沛,如let age:number = 19;
string為字符串類型港柜,如let name:string='zhangsan';
數(shù)組類型痊项,如let list: data:number[]=[1,2,3];
多維數(shù)組:如let dataArr:number[][]=[[1,2,3],[4,5,6]];
元組類型,如let x: [ string, number ] = [ "hello", 10 ]
枚舉類型萝喘,如enum Sex {
MALE,
FAMALE,
UNKNOW
}
let sex:Sex=Sex.FAMALE;
any為任意類型淮逻,如let unknow: any = 10; unknow= "不確定的類型";
void為空類型,如let sky: void = undefined
null和undefined
never表示沒有值的類型阁簸,如function fn(msg: string): never { throw new Error(msg); }
聯(lián)合類型可以用 | 隔開爬早,如let da:number | string=12;表示可以是number或string類型
二、例子:
// boolean
const flag:boolean=true;
console.log(flag);
// 數(shù)值
const age:number=19;
console.log(age);
// 字符串
let name:string='zhangsan';
name='100';
console.log('name:', name);
let lang:any='ts';
lang=50;
console.log(lang);
// 對象
const obj:object={};
console.log(obj);
// 數(shù)組
const data:number[]=[1,2,3];
data[0]=10;
data[3]=40;
const data2:string[]=['one','two','three'];
// 多維數(shù)組
const dataArr:number[][]=[[1,2,3],[4,5,6]];
// 枚舉
enum Sex {
MALE,
FAMALE,
UNKNOW
}
let sex:Sex=Sex.FAMALE;
console.log(sex);
console.log(Sex[sex]);
switch (+sex) {
case Sex.MALE:
console.log('男');
break;
case Sex.FAMALE:
console.log('女');
break;
case Sex.UNKNOW:
console.log('未知');
break;
default:
break;
}
// 聯(lián)合類型
let da:number | string=12;
da='qqq';
console.log(da);
let da2:number | number[]=4;
da2=[1,5,6];
console.log(da2);