Reputation: 4561
Consider the function argument type for an array of strings as options:
function (values: ("option1" | "option2" | "option3")[]) {
....
}
I am after a generic type where the function could receive this:
function (values: ("option1" | "option2")[]) {
....
}
And this:
function (values: ("option1" | "option2" | "option3" | "option4" | "option5")[]) {
....
}
What would be the type for a generic string array that holds a type that is the union type of any of its values? Asked in different way, the type so that the function could receive any string array with each of its items being of the union type of any of the items of the array?
Upvotes: 0
Views: 278
Reputation: 187034
It's a little hard to follow you here, but I believe you are after SomeArray[number]
. Since arrays have numeric indices, indexing an array by number
returns a union of all its values.
type MyArray = ['a', 'b', 'c']
type MyUnion = MyArray[number] // 'a' | 'b' | 'c'
Upvotes: 1