网站开发与设计实训心得一千字,wordpress 课程管理系统,深圳住房建设局网站申报,电子商务的发展趋势在很多场景下#xff0c;interface 和 type都能使用#xff0c;因此两者在很多时候会被混淆#xff1a;
接口可以通过之间的继承#xff0c;实现多种接口的组合 使用类型别名也可以实现多种的#xff0c;通过连接,有差异#xff1a;
子接口中不能重新覆盖父接口中…在很多场景下interface 和 type都能使用因此两者在很多时候会被混淆
接口可以通过之间的继承实现多种接口的组合 使用类型别名也可以实现多种的通过连接,有差异
子接口中不能重新覆盖父接口中的成员类型别名会合并相同类型的成员进行交叉。若一个number,一个字符串就无法赋值。如下
type User1 {name:number,}
type User2 {name:string
}type MyUser User1 User2
let user:MyUser {name:zdy
} 交叉平时用的不算很多接口的继承用的多一些
推荐使用(接口)interface。
1.类型别名
type 会给一个类型起个新名字。 type 有时和 interface 很容易混淆但是不同的是type可以作用于原始值基本类型联合类型元组以及其它任何你需要手写的类型。
起别名不会新建一个类型它创建了一个新名字来引用那个类型。给基本类型起别名作用不大但是可以做为文档的一种形式使用.
type Name string; // 基本类型type NameFun () string; // 函数type NameOrRFun Name | NameFun; // 联合类型function getName(n: NameOrRFun): Name {if (typeof n string) {return n;} return n();
}
2.接口
接口使用 interface 关键字进行定义, 在没有设置特殊标识时, 使用接口作为类型定义变量必须将接口内的属性完整定义, 不然就会报错 . 接口定义对象默认属性是可以更改的接口相当于是一种契约
interface Person {name: stringage: number
}
const person: Person {name: a,age: 1
}
person.age 2
console.log(person) // {name: a, age: 2}一点小细节
他们在约束函数时候形式有点不大一样一个是冒号一个是箭头
// interface
interface SetPoint {(x: number, y: number): void;
}// type
type SetPoint (x: number, y: number) void;
接口和类型别名最大区别 接口可以被类实现而类型别名不可以 当然接口也可以继承类表示类中的所有成员都在接口中
// 火圈接口interface IFireShow{singleFire():void;doubleFire():void;
}// 动物类
abstract class Animal {abstract type:string;constructor(public name:string,public age:number){}sayHello(){console.log(大家好我是${this.name},我今年${this.age}岁);}
}class Lion extends Animal implements IFireShow{type:string 狮子;singleFire(){console.log(${this.name}我会喷单火圈);}doubleFire(){console.log(${this.name}我会喷双火圈);}
}