interface {} 就是说明书,封装的组件,你就是生产厂家,生产的东西要给别人用
    d.ts就是说明书文件,是给 js打补丁用的

    1. Object类型,对对象的形状 shape进行描述,只能做类型的静态检查
      1. interface定义形状和约束,关注值的形状
    2. 对 class类进行抽象
    3. Duck Typing 鸭子类型
    4. 结构化子类型 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); }

    1. <a name="kJABI"></a>
    2. ## 可选属性
    3. ```typescript
    4. // 可选属性
    5. interface Animal{
    6. color?:string;
    7. size?:number;
    8. }
    9. function creatAnimal(ani:Animal):{ color:string;size:number}{
    10. var aniTemp ={
    11. color:"yellow",
    12. size:100
    13. }
    14. if(ani.color){
    15. aniTemp.color=ani.color;
    16. }
    17. if(ani.size){
    18. aniTemp.size=ani.size;
    19. }
    20. return aniTemp;
    21. }

    编译成js

    1. function creatAnimal(ani) {
    2. var aniTemp = {
    3. color: "yellow",
    4. size: 100
    5. };
    6. if (ani.color) {
    7. aniTemp.color = ani.color;
    8. }
    9. if (ani.size) {
    10. aniTemp.size = ani.size;
    11. }
    12. return aniTemp;
    13. }