今天面试遇到一道题,想了很久也没有想出来
type Events = {
ready: void
error: Error
reconnecting: { attempt: number; delay: number }
}
type RedisClient = {
on<T extends keyof Events>(event: T, f: (arg: Events[T]) => void): void
}
let aa: RedisClient =
这里要怎么写
aa.on('ready', () => {
console.log(12312)
})
我是这样写的,但是报错了
let aa: RedisClient = {
on: function (event, f): void {
switch (event) {
case 'ready':
f()
break
case 'error':
f(new Error())
break
case 'reconnecting':
f({ attemp: 1, delay: 1 })
break
default:
break
}
}
}
f()的参数有问题,想请教一下大家应该如何写?