interface {} 就是说明书,封装的组件,你就是生产厂家,生产的东西要给别人用
d.ts就是说明书文件,是给 js打补丁用的
- Object类型,对对象的形状 shape进行描述,只能做类型的静态检查
- interface定义形状和约束,关注值的形状
- 对 class类进行抽象
- Duck Typing 鸭子类型
- 结构化子类型 structural subtyping ```typescript // readOnly 用在属性上面;const用在变量上面 interface DataProps { name: string; age: number; like?: string; // ?: 可选属性 fun(): string; run(): void; readonly id: number // readonly 只读属性 }
const user = { id: 2, name: ‘lucy’, age: 200, fun (value) { return value } }
user.id // 报错,因为是 readonly
interface IPerson { // I 标识 Interface name: string; age: number; }
const user: IPerson = { // 定义的属性不能多也不能少,多一个,少一个都会报错 name: ‘lucy’, age: 20 }
interface Person{ name:string; age:number; }
function creatPerson(per:Person){ console.log(per.age); }
<a name="kJABI"></a>## 可选属性```typescript// 可选属性interface Animal{color?:string;size?:number;}function creatAnimal(ani:Animal):{ color:string;size:number}{var aniTemp ={color:"yellow",size:100}if(ani.color){aniTemp.color=ani.color;}if(ani.size){aniTemp.size=ani.size;}return aniTemp;}
编译成js
function creatAnimal(ani) {var aniTemp = {color: "yellow",size: 100};if (ani.color) {aniTemp.color = ani.color;}if (ani.size) {aniTemp.size = ani.size;}return aniTemp;}
