【发布时间】:2020-09-25 23:11:36
【问题描述】:
我有这种情况:
// Application.ts
import MicroEvent from 'microevent-github'
class Application {
// stuff...
something() {
// It is also saying "trigger" is undefined,
// but it IS defined, MicroEvent defined it.
this.trigger('foo')
}
}
// get the `bind`, `unbind`, and other methods from MicroEvent
MicroEvent.mixin(Application)
const app = new Application()
const handleFoo = () => console.log('foo')
// try to use them, get the squiggly errors saying
// `bind` doesn't exist on Application, etc.
application.bind('foo', handleFoo)
application.unbind('foo', handleFoo)
我已将MicroEvent“混入”到我的应用程序中,从而为对象添加了一些方法。但是,VSCode 抱怨 bind 和 unbind 在 Application 实例上不存在......但它确实存在,我如何告诉 typescript 接受这个?
添加这个不起作用:
type Application = {
bind: (eventType: string, callback: () => void) => void
unbind: (eventType: string, callback: () => void) => void
trigger: (eventType: string) => void
}
【问题讨论】:
标签: typescript class mixins