我有这个自定义事件设置,它可以与TypeScript 2.5.3
一起使用,但是当我更新到2.6.1
时,我收到一个错误
window.addEventListener('OnRewards', (e: CustomEvent) => {
// my code here
})
[ts]类型'(e:"自定义事件)=〉void"不能赋给类型为"EventListenerOrEventListenerObject"的参数。
类型"(e:自定义事件)=〉void"不能赋给类型"EventListenerObject"。
类型"(e:)"中缺少属性"handleEvent"自定义事件)=〉无效"。
我不太确定该怎么做才能解决这个问题。
6条答案
按热度按时间nxagd54h1#
这是由于TypeScript v2.6中添加的
--strictFunctionTypes
编译器标志的行为所致。(e: CustomEvent) => void
类型的函数不再被视为EventListener
的有效示例,EventListener
采用Event
参数,而不是CustomEvent
参数。所以解决这个问题的一个方法是关闭
--strictFunctionTypes
。另一种方法是传入一个函数,该函数接受一个
Event
,然后通过一个类型保护将范围缩小到CustomEvent
:第三种方法是使用
addEventListener()
的另一个重载:如果
type
参数是已知事件类型(K extends keyof WindowEventMap
)的名称,如"onclick"
,则listener
函数将期望其参数为该缩小的事件类型(WindowEventMap[K]
)。问题是"OnRewards"
不是已知事件类型...除非您使用declaration merging来 * 使 * 它已知:或者,如果你在一个模块中(任何包含
export
的模块),使用全局扩展:然后像以前一样使用您的代码:
所以,这些就是你的选择。你想选择哪一个取决于你。希望这对你有帮助;祝你好运!
xytpbqjk2#
在jcalz's excellent answer的基础上,您还可以使用类型Assert来简化:
cbeh67ev3#
您还可以选择:
yfwxisqw4#
我根据@jcalz的答案创建了一个泛型函数
yh2wf1be5#
是的,另一种满足编译器的方法是强制转换事件对象,例如
yourFuctionName(e: Event){ const event = e as CustomEvent <SegmentChangeEventDetail>; console.log(event.detail)}
3hvapo4f6#
我在“click”事件上遇到了这个问题,这是一个意外的事件,因为它是一个本机事件,结果你不能只使用内联的document.querySelector,因为推断的类型是
Element | null
,而不是HTMLElement | null
,所以你必须将它声明为一个HTMLElement,这意味着它失败了:但是当您将其声明为HTMLElement时,它可以正常工作:
我知道这个问题是关于自定义事件的,但这是在“单击”事件上搜索相同错误时的第一个结果。