一、基础类型
//布尔值 let isDome:boolean = true; //数字 let age:number = 18; //字符串 let name:string = '小王'; //数组 let arr: number[] = [1,2,3]; let arr: Array<number> = [1,2,3]; //元祖 已知元素数量和类型的数组 let x:[string, number] = ['小王', 18] //枚举 enum Color {Red=1, Blue, Green}; let c: Color = Color.Red; let colorName: string = Color[1]; //Any 任何类型 let notSure: Any = 4; notSure = 'string'; notSure = false; //Void 没有任何类型 function show():void { console.log('void'); } let unable: void = undefined; unable = null; //undefined 和 null let x:undefined = undefined; let y:null = null; //Never never类型是那些总是会抛出异常或根本就不会有返回值的函数 function error(msg: string):never { throw new error(msg); }
二、类型断言
let str: any = 'xxxxxxxxxxxxxxxx'; let num: number = (<string>str).length; //等价于 let num: number = (str as string).length;
作者:大富_大贵
链接:https://www.jianshu.com/p/c5772fe24f56