Trying to properly define an array structure like this:
type HeadItem = { type: "Head" }
type RestItem = { type: "Rest" }
const myArray = [{ type: "Head" }, { type: "Rest" }, { type: "Rest" }]
The number of rest elements can vary, but the first element always must be a head element. One way to define the array is shown below, however, the ...rest
ends up with an incorrect type of (HeadItem | RestItem)[]
.
type MyArr = [HeadItem] & RestItem[];
const [head, ...rest] = myArray as MyArr;
What would be the correct type for MyArr
so that the ...rest
is correctly deduced as just RestItem[]
?