How to use the spread operator in a typescript tuple?

Viewed 2051

I want to specify how the rest of the array should look without knowing how many values the array will have. How can I achieve something like this? (playground version here):

type numStrArr = [number, ...string];

let arr: numStrArr = [1, 'hi', 'other string'];

The above code throws the error Type expected. at the spread operator, however.

I'm aware that there was a proposal for this, but how can I achieve similar behavior now?

1 Answers

I know this question is 3 years old, but the feature is supported now. All you need to do is change the type to a type that can be spread. Read more about it here:


// no more error...
type numStrArr = [number, ...string[]];

Related