【发布时间】:2021-07-04 12:32:54
【问题描述】:
我目前在 nextJS 项目中使用 TypeScript。 我正在异步使用cdn版本的flowplayer,它使用新属性扩展了event.target。
问题是当我尝试构建时出现错误: 类型错误:“EventTarget & HTMLInputElement”类型上不存在属性“currentTime”。
我需要将它与以下属性相交:currentTime、duration、opts。 这是我尝试做的:
type FlowPlayerEvent<T extends EventTarget> = Event & {
target: T;
currentTime: Number;
duration: Number;
opts: Object;
};
这是我的事件处理程序
function stateHandler(ev : Event) {
const Event: FlowPlayerEvent = ev.target;
if (ev.type === 'pause') { console.log('paused'); }
if (ev.type === 'playing') { console.log(`Playing at ${Event.currentTime}, duration is: ${Event.duration}, video ID is: ${Event.opts.metadata.id}`); }
if (ev.type === 'timeupdate') { console.log(Event.currentTime); }
if (ev.type === 'ended') { console.log('The end'); }
}
当我将鼠标悬停在 FlowPlayerEvent 上时,我得到了: 通用类型“FlowPlayerEvent”需要 1 个类型参数。ts(2314)
在这种情况下扩展它的正确方法是什么?
提前致谢!
【问题讨论】:
-
const Event: FlowPlayerEvent = ev.target;您似乎对什么是事件以及什么是目标感到困惑。我不熟悉flowplayer。它有打字稿类型吗?目标应该是HTMLVideoElement还是其他?
标签: typescript next.js flowplayer