rustyBucketBay
rustyBucketBay

Reputation: 4561

Generic string array where each of the items is of the array values union type

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

Answers (1)

Alex Wayne
Alex Wayne

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

Related Questions