1. interface Person{
    2. name: String,
    3. age: Number,
    4. sex: String
    5. }
    6. const getPerson = (person: Person) => {
    7. let {name, age} = person
    8. if(name !== 'james' || age < 18) console.log('淘汰1')
    9. }
    10. const getPerson2 = (person: Person) => {
    11. let {name, age} = person
    12. if(name !== 'westbrook' || age < 18) console.log('淘汰2')
    13. }
    14. let hero = {
    15. name: 'james',
    16. age: 35,
    17. sex: '男'
    18. }
    19. getPerson(hero)
    20. getPerson2(hero)

    一:interface和type的区别

    • type可以定义为普通类型,比如string。interface必须定义为对象。 ```javascript type Girl1 = stirng;

    interface Person{ name: String, age: Number, sex: String }

    1. 二:接口定义
    2. ```javascript
    3. interface Person{
    4. name: String,
    5. age: Number,
    6. // 可选属性
    7. sex?: String,
    8. // 任意属性,属性的名字是字符串类型,属性的值可以是任何类型
    9. [propname: string]: any,
    10. // 属性里的方法,返回值是string
    11. say(): string
    12. }

    三:接口的继承

    1. interface Teacher extends Girl {
    2. teach(): string;
    3. }