[英]Do I have to use an interface to define the elements of an object with typescript?
[英]How do I define an typescript interface for a constructor that also is an object?
我收到此错误:
This expression is not constructable.
Not all constituents of type '(new (arg0: HTMLDivElement, arg1: object) => Embed) | { VIDEO_READY: string; }' are constructable.
Type '{ VIDEO_READY: string; }' has no construct signatures.
这是我的界面:
declare global {
interface Window {
Twitch: {
Embed: new (arg0: HTMLDivElement, arg1: object) => Embed;
} | {
Embed: {
VIDEO_READY: string;
};
};
}
}
这是用法:
embed = new window.Twitch.Embed(video.value, {
video: props.twitch,
});
embed.addEventListener(window.Twitch.Embed.VIDEO_READY, () => {
player = embed.getPlayer();
player.pause();
resizeIframe();
});
使用 Twitch API window.Twitch.Embed
是一个构造函数和一个对象......
我会将它定义为单个对象,而不是将其拆分为一个联合对象。
// Your usage
embed = new window.Twitch.Embed(htmlDiv, {
video: 123,
});
embed.addEventListener(window.Twitch.Embed.VIDEO_READY, () => {
// ...
});
// The definition
declare global {
interface Window {
Twitch: {
Embed: {
new(arg0: HTMLDivElement, arg1: object): Window['Twitch']['Embed'];
VIDEO_READY: string;
}
}
}
}
// Some excess irrelevant stuff
declare var embed: any;
declare var htmlDiv: HTMLDivElement;
export const exportSomething = 123;
在我看来,您正在尝试在 Angular/Typescript 项目中使用 Twitch 交互式播放器。
我最近遇到了同样的问题,这就是为什么我决定为 Twitch 交互式嵌入播放器实现一个 Typescript 包装器。 这与 Angular 100% 兼容,因此如果您仍然需要一种方法来实现这一点,请查看此存储库。 希望有帮助^ ^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.