Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

263
Vistas
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 {
      ///
   }
}
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can use instanceof:

getRoute(video: LiveVideo | RecordedVideo) {
  if (video instanceof LiveVideo) {
    // "video" is of type LiveVideo here...
  } else {
    // ...
  }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

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
   }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

type LiveVideo = {
    getLiveID: Function,
    id: string
}

type RecordedVideo = {
    id: string
}

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

        ///
    } else {
        ///
    }
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda