函数内的this声明
interface DB {
filterUser(filter: (this: User)=> boolean): User[]
}
实例:
interface User {
admin: boolean
}
interface DB {
filterUsers(filter: (this: User) => boolean): User[]
}
const db: DB = {
filterUsers: (filter: (this: User) => boolean) => {
let user1: User = {
admin: true
}
let user2: User = {
admin: false
}
return [user1, user2]
}
}
const admins = db.filterUsers(function (this: User) {
return this.admin
})
网友评论