Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!