元组:
let arr : [string, number] = ['hello', 3];
let arr2 : [number, boolean?] = [44];//问号可选的
let arr3 : [number, ...string[]] = [34, 'a', 'b', 'c'];//任意多个字符串,也可以没有
let arr4 : [number, ...string[]] = [34];
枚举:
//enum 数字枚举(有反向映射)
enum Direction {
Up,
Down,
Left,
Right
}
console.log(Direction.Up)
function walk(str : Direction) {
if (str === Direction.Up) {
console.log('上');
} else if (str === Direction.Down) {
console.log('下');
} else if (str === Direction.Left) {
console.log('左');
} else if (str === Direction.Right) {
console.log('右');
} else {
console.log('未知方向');
}
}
walk(Direction.Down)
// enum 字符串枚举(没有反向映射)
enum Source {
Inside = "inside",
Outside = "outside"
}
console.log(Source);//{Inside: 'inside', Outside: 'outside'}
type:
// type
type Status = number | string; // |管道符
type Gender = '男' | '女';
function printStatus(data : Status) : void {
console.log(data);
}
function printGender(data : Gender) : void {
console.log(data);
}
printStatus(200);
printStatus('200')
printGender('女')
printGender('男')
// 面积
type Area = {
width : number;
height : number;
}
// 地址
type Address = {
room : number,//房间号
cell : number | string,//单元号
}
type House = Area & Address;
const house : House = {
width: 100,
height: 100,
room: 801,
cell: 2
}