【问题标题】:Why do I get "'VideoSource' only refers to a type, but is being used as a value here." [duplicate]为什么我得到“'VideoSource' 仅指一种类型,但在这里被用作一个值。” [复制]
【发布时间】:2020-06-05 21:55:44
【问题描述】:

在第一个 if 检查中我没有收到错误,如果我收到错误,则在 else 中进行操作

'VideoSource' 仅指一种类型,但在这里用作值。

let element:VideoSource|VideoTrack;
element = {
    src: '',
    videoType: 'video/mp4'
}

if (element instanceof  VideoTrack) {

}
else if (element instanceof  VideoSource) {

}

export interface VideoSource {
    src:string;
    videoType:'video/mp4'|'video/webm'|'video/ogg';
}

export interface VideoTrack {
    src:string;
    kind: 'subtitles'|'captions'|'chapters'|'descriptions'|'metadata'
    label?:string;
    srclang?:string;
    IsDefault?:'default';
}

【问题讨论】:

    标签: typescript


    【解决方案1】:

    查看answer 到 typescript GitHub 上的问题。 还有这个answer

    基本上,因为接口只存在于编译时并在编译后被删除,所以您不能使用它们在运行时进行检查。请注意,类是可能的。

    【讨论】:

    • 但是为什么 VideoTrack 界面可以工作呢?
    • 这似乎是一个built in type,当我将它悬停在我的IDE(vs代码)中时,它显示它有一个原型和构造函数,所以它是一个类并且存在于运行时.
    • 删除你的 VideoTrack 界面,你会发现它仍然有效
    猜你喜欢
    • 2017-08-16
    • 2018-01-14
    • 2019-10-08
    • 2019-07-30
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    相关资源
    最近更新 更多