The tuple tuple type allows representing an array with a known number and type of elements, 各元素的类型不必相同. For example, you can define a pair of tuples whose values are string and number. When accessing an element with a known index, the correct type is obtained.
// 数字的枚举enum Color {
Red,Green,Blue}// 默认是从0开始,逐个递增let c: Color = Color.Green;// 1enum Color {
Red =1,Green,Blue}// 可以指定开始的数字,后面也是逐个递增let c: Color = Color.Green;// 2enum Color {
Red =1,Green =4,Blue =8}// 可以全部手动指定// 字符串的枚举,后面必须都要指定,否则报错enum Sex{
male ='男',female ='女'}let sex: Sex = Sex.female;// 女enum Sex{
male ='男',female}// female必须指定,否则报错// 通过枚举的值反推映射的名字enum Color {
Red =1, Green, Blue}let colorName:string= Color[2];// Green
any type: Indicates any type. When the variable type is not clear, you do not want the type checker to check these values but directly let them pass the check at the compilation stage
const name:any='wangjiajia'
void type: Contrary to any type, it means that there is no type, and it is generally used for a function without a return value.
functionwarnUser():void{
console.log("This is my warning message");}// 声明一个void类型的变量没有么意义,因为只能赋值undefined和nullconst name: viod =undefinedconst name: viod =null
null and undefined:两者有各自的类型,默认是所有类型的子类型
let u:undefined=undefined;let n:null=null;let name:string=undefined|null
never type: 表示那些永不存在的值的类型, the never type is the return value type of a function expression or an arrow function expression that always throws an exception or has no return value at all;never类型是任何类型的子类型,也可以赋值给任何类型,没有类型是never的子类型或可以赋值给never类型,即使是any也不可以赋值给never。
// 返回never的函数必须存在无法达到的终点functionerror(message:string):never{
thrownewError(message);}// 推断的返回值类型为neverfunctionfail(){
returnerror("Something failed");}// 返回never的函数必须存在无法达到的终点functioninfiniteLoop():never{
while(true){
}}let name:neverlet age:any=18
name = age // any类型也不可以赋值给never类型
3. Type assertion
<type> value (cannot be used in TSX)
let name:any="this is a string";// 将name断言成string类型let nameLength:number=(<string>someValue).length;
value as type
let name:any="this is a string";// 将name断言成string类型let nameLength:number=(someValue asstring).length;
Variable!: Indicates that the variable is not null or undefined
name! 断言name不为null或者undefined
4. Type aliases
Defined by type:
typeuser={
name:string,
age:number}// 类型扩展typeuser2= user &{
address:string}