I have an array of strings – all possible standart event names. I use it to create union type, where every element is string. Now I can use that union for strings, but not for strings[].
What the difference between string and string[] in this situation?
/* @flow */
type T = 'a' | 'b'
const a: T = 'a'
function f(s: string) {}
f(a) // ok
const b: T[] = ['b']
function fa(arr: string[]) {}
fa(b) // string enum is incompatible with string
Why flow think b could have anything not from T?
Array of strings is array of strings, not array of literals. It's the same as
const a: string = 'a';
const b: 'a' | 'b' = a;
you can't assign string to less common type without refinement.
But I think my case have reverse direction. Function fa requires string type (array of strings), which is more common than union of strings T, right? I mean T is a subset of string type.
Anyway, how to handle my case? I have an array of string constants, and I need to pass them to function which accept an array of any strings.
@Diokuz Possibly because you can mutate it. With $ReadOnlyArray all works fine.
https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVAXAngBwKZgAqYAvGAOQCG5YAPhQEbnoDGcAdgM4ZgMBcYACQAlPJQAmAeXYwsAQQBOCylgA8hAHykwAbXJMAuqigBXdiwwBLDmCiUAFJSUCRYqTPlKVq7gsvsAcw0ASjAAbwBfYwcGUOBgMF9-ALA8dhMAWzBLTmzzOAycSisGGAIESwwAC0SMP0CgA
Most helpful comment
@Diokuz Possibly because you can mutate it. With
$ReadOnlyArrayall works fine.https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVAXAngBwKZgAqYAvGAOQCG5YAPhQEbnoDGcAdgM4ZgMBcYACQAlPJQAmAeXYwsAQQBOCylgA8hAHykwAbXJMAuqigBXdiwwBLDmCiUAFJSUCRYqTPlKVq7gsvsAcw0ASjAAbwBfYwcGUOBgMF9-ALA8dhMAWzBLTmzzOAycSisGGAIESwwAC0SMP0CgA