i'm having a tough time figuring out how to make my and function here: it has a simple job. it must accept a bunch of items, and concatenate them into an array that starts with "and"
// the items 'and' may accept
type Item = false
| {type: "alpha", a: boolean}
| {type: "bravo", b: number}
// 'and' function adds items to an array
function and<Items extends Item[]>(
...items: Items
): ["and", ...Items] {
return ["and", ...items]
}
// calling 'and' with a few items
const [op, item1, item2, item3] = and(
false,
{type: "alpha", a: true},
{type: "bravo", b: 3.14},
)
// verifying the types are preserved
op //> ✔ type "and"
item1 //> ✘ type Item, expected false
item2 //> ✘ type Item, expected {type: "alpha", a: true}
item3 //> ✘ type Item, expected {type: "bravo", b: 3.14}
the problem is that item1, item2, and item3 are all losing their original specific type information, thus my and function is actually a destructive operation on types — instead, i need the types preserved
is there a different approach? thanks!