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

265
Vistas
La propiedad no existe en el tipo de error cuando la propiedad existe

Recibí el error mencionado anteriormente al crear el siguiente código TypeScript. Obviamente, el problema es que las URL, el tipo y las subpropiedades no existen en un parámetro del tipo string[], pero no se leerían si ese fuera el tipo del parámetro de destino. Soy extremadamente nuevo en TypeScript, este es mi primer intento de trabajar con él, por lo que probablemente me esté perdiendo algo relativamente básico.

 interface targeterInterface { urls: string | string[]; type?: string; sub?: string; } function exp(targeter: string | targeterInterface | string[]) { let target: string[] | string; let type: string; let sub: string; if (typeof targeter === "object") { target = targeter.urls; type = targeter.type; sub = targeter.sub; } else if (typeof targeter === "string") { target = targeter; } else { console.log("It's an array!"); } }

¡Gracias por cualquier ayuda!

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

0

Si no es una cadena o una matriz , de lo contrario es un objeto

 if (Array.isArray(targeter)) { // Array } else if (typeof targeter === "string") { // string } else { // Instance of targeterInterface }
about 4 years ago · Juan Pablo Isaza Denunciar

0

Puede usar una característica de TypeScript llamada función de protección de tipos para verificar cuál es el tipo del argumento. https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards

 interface targeterInterface { urls: string | string[]; type: string; // had to change since, the type in the function is not optional sub: string; // had to change since, the sub in the function is not optional } function isTargeterInterface(item: unknown): item is targeterInterface { return (item as targeterInterface).type !== undefined; } // this is a type guard function function exp(targeter: string | targeterInterface | string[]) { let target: string[] | string; let type: string; let sub: string; if (isTargeterInterface(targeter)) { // calling a function to check if it implements interface provided above target = targeter.urls; type = targeter.type; sub = targeter.sub; } else if (typeof targeter === "string") { target = targeter; } else { console.log("It's an array!"); } }

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