How can you create a type in TypeScript that only accepts arrays with two or more elements?
needsTwoOrMore([\"onlyOne\"]) // should have error
needsTwoOrMore
This is an old question and the answer is fine (it helped me as well), but I just stumbled across this solution as well while playing.
I had already defined a typed Tuple (type Tuple), and then below that, I had defined array of two or more as described above (type ArrayOfTwoOrMore).
It occurred to me to try using the Tuple structure in place of { 0: T, 1: T }, like so:
type ArrayOfTwoOrMore
And it worked. Nice! It's a little more concise and perhaps able to be clearer in some use-cases.
Worth noting is that a tuple doesn't have to be two items of the same type. Something like ['hello', 2] is a valid tuple. In my little code snippet, it just happens to be a suitable name and it needs to contain two elements of the same type.