0

In the first if check I don't get an error, nut in the else if I get

'VideoSource' only refers to a type, but is being used as a value here.

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';
}
Ilias Tsakiridis
  • 58
  • 1
  • 2
  • 10

1 Answers1

1

Check out this answer to an issue on the typescript GitHub. Also this answer.

Basically, because Interfaces only exist at compile-time and are removed after compilation, you can't use them to do checks at run-time. Note that it is possible with classes.

niiyeboah
  • 1,320
  • 9
  • 18
  • But why does the _VideoTrack_ interface work? – Ilias Tsakiridis Feb 21 '20 at 13:15
  • 1
    That appears to be a [built in type](https://developer.mozilla.org/en-US/docs/Web/API/VideoTrack), when I hover it in my IDE (vs code) it shows that it has a prototype and constructor, so it is a class and is present at run-time. – niiyeboah Feb 21 '20 at 13:19
  • 1
    Remove your _VideoTrack_ interface and you'll see that it still works – niiyeboah Feb 21 '20 at 13:23