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?