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

195
Vistas
Typescript: how to name array of strings that could be undefined?

I'm using Typescript in my React Native app and I define the following array:

const arr: string[] = Platform.select({
  ios: ["a", "b", "c"],
  android: ["a", "b", "c"]
})

VsCode underlines arr and tells me Type 'string[] | undefined' is not assignable to type 'string[]', so I changed it to

const arr: string[] | undefined = Platform.select({
  ios: ["a", "b", "c"],
  android: ["a", "b", "c"]
})

VsCode removes the underline on arr in the definition, but when I use it in my code it underlines it there and says Argument of type 'string[] | undefined' is not assignable to parameter of type 'string[]'

I think the problem is that Platform.select() might return undefined, but then I'm using arr in my code for operations that require it to have type string[] instead of string[]. How can I modify my code to remove all these warnings?

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

0

You can give an empty array as the default value when Platform.select returns undefined using nullish coalescing operator.

This would also work

const arr: string[] = Platform.select({
  ios: ["a", "b", "c"],
  android: ["a", "b", "c"]
}) ?? []
about 4 years ago · Juan Pablo Isaza Denunciar

0

This may suffice:

const arr: string[] = Platform.select({
  ios: ["a", "b", "c"],
  android: ["a", "b", "c"]
}) as unknown as string[]

But, I would be cautious. It's usually better to listen to Typescript and adjust your design accordingly.

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