sfletche
sfletche

Reputation: 49714

Flow type annotation for Array of one type or another (but not both)

I have a function that can take an Array of numbers or an Array of strings (but not an Array of numbers and strings).

In other words, the Array might resemble [1, 2, 3] OR ['1', '2', '3']

but NOT [1, '2', 3]

Right now I declare it like so

function fn<T>(options: Array<T>): Array<T> { ... }

but this ^ would allow for Array of any single type (strings, numbers, objects, whatever).

Looking at the docs on Flow's generic types, the only alternative I can think of looks like this

function fn<T: string | number>(options: Array<T>): Array<T> { ... }

but that ^ allows for an Array that contains both strings and numbers, which as explained above, is not what I want...

Surely, there's a way to flow type an Array of strings OR an Array of numbers...?

Upvotes: 0

Views: 477

Answers (1)

Jonas Wilms
Jonas Wilms

Reputation: 138267

Array<string> | Array<number>

Which reads as an array of strings or an array of numbers

Upvotes: 3

Related Questions