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

140
Vistas
TypeScript Props as Array of Objects or string

I will receive a props url that could be string or array of objects like:

type VideoSrcProps = {
    url?: string;
    day?: string;
  };
  interface OwnProps {
    url: VideoSrcProps[] | string;
  }

But my implementation doesn't work because I receive Property 'url' does not exist on type 'string | VideoSrcProps' Property 'url' does not exist on type 'string'

How it should be changed?

More code:

I'm checking that what will came array of string like

const srcIsArray = Array.isArray(url) ? true : false;
const notSingleArrayVideo = srcIsArray && url.length > 1 ? true : false;

And then

useEffect(() => {
    const preloadVideos = async () => {
      if (srcIsArray && notSingleArrayVideo) {
        const preloadedVideos = [];
        for (const video of url) {
          const response = await axios({
            url: video?.url,
            method: 'GET',
            responseType: 'blob',
          });
          const downloadedVideo = window.URL.createObjectURL(new Blob([response.data]));
          preloadedVideos.push({ url: downloadedVideo });
        }

        if (preloadedVideos.length === url.length) {
          setPreloadedVideos(preloadedVideos);
        }
      }
    };
    if (srcIsArray && notSingleArrayVideo) {
      preloadVideos();
    }
  }, []);

The problem is with url: video?.url

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You have an error on this:

const notSingleArrayVideo = srcIsArray && url.length > 1 ? true : false;

TS will not narrow your type when using a stored value out of a ternary.

You should use the condition directly in your if statement, or do an if and return.

type VideoSrcProps = {
    url?: string;
    day?: string;
  };


interface OwnProps {
  url: VideoSrcProps[] | string;
}


function test (url: OwnProps) {
  if (Array.isArray(url.url)) {
    for (const video of url.url) {
      console.log(video.url);
    }
  }
}
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