Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Typescript: How to accept 2 different types

I have a method getRoute that takes a video. The video is either of LiveVideo type or RecordedVideo type. My problem is that getLiveID field is only in LiveVideo so I am getting the editor issue property getLiveID does not exist on type RecordedVideo. What is the way around this? I want to use 1 method to handle both and handle the conditional logic within.

getRoute(video: LiveVideo | RecordedVideo) {
   if (video.getLiveID) {

     ///
   } else {
      ///
   }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can check which type you have using in. For example:

getRoute(video: LiveVideo | RecordedVideo) {
   if ("getLiveId" in video) {
     // treat as LiveVideo
   } else {
     // as RecordedVideo
   }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading