Flow: enum string and array of strings

Created on 21 Feb 2018  Â·  3Comments  Â·  Source: facebook/flow

TRY

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?

Most helpful comment

All 3 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pelotom picture pelotom  Â·  3Comments

philikon picture philikon  Â·  3Comments

cubika picture cubika  Â·  3Comments

glenjamin picture glenjamin  Â·  3Comments

Beingbook picture Beingbook  Â·  3Comments