interface Person{
name: String,
age: Number,
sex: String
}
const getPerson = (person: Person) => {
let {name, age} = person
if(name !== 'james' || age < 18) console.log('淘汰1')
}
const getPerson2 = (person: Person) => {
let {name, age} = person
if(name !== 'westbrook' || age < 18) console.log('淘汰2')
}
let hero = {
name: 'james',
age: 35,
sex: '男'
}
getPerson(hero)
getPerson2(hero)
一:interface和type的区别
- type可以定义为普通类型,比如string。interface必须定义为对象。 ```javascript type Girl1 = stirng;
interface Person{ name: String, age: Number, sex: String }
二:接口定义
```javascript
interface Person{
name: String,
age: Number,
// 可选属性
sex?: String,
// 任意属性,属性的名字是字符串类型,属性的值可以是任何类型
[propname: string]: any,
// 属性里的方法,返回值是string
say(): string
}
三:接口的继承
interface Teacher extends Girl {
teach(): string;
}