TypeScript

函数内的this声明

2023-01-04  本文已影响0人  我的袜子都是洞

函数内的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
})
上一篇下一篇

猜你喜欢

热点阅读