TypeScript type类型 自定义类型
一、作用
自定义类型,注意首字母大写
二、语法
1、基本类型
let a:Flag = true console.log(a);
一般不这么用,无意义
2、联合类型(或) |
a、语法
tyep 类型名= 类型(字面量) | 类型(字面量)
满足其中一个即可
b、案例
// 定义类型 Flag type Flag = true|false // 使用类型 let isFlag:Flag = true console.log(isFlag); // 定义类型Num type Num = 0|2|4|6|8 let a:Num = 2 console.log(a); type Sex="男"|"女"|number let s:Sex = 0 console.log(s);
3、交叉类型(且) &
a、语法
type 类型名 = 类型&类型&类型
所有类型都要有
b、案例
type Person = {name:string, age:number, sex:string} type Work ={occupation:string, salary:number} type Coder = Person & Work let coder:Coder={ name:'jojo', age:8, sex:"男", occupation:"码农", salary:2800 } console.log(coder);