I'm struggling to understand how to use variadic tuple types in Typescript. I've tried to read through the docs, and some issues on the GitHub, but examples always are a bit weirder than "super basic", so i wondered if someone could help me type a couple of "super basic" ones so I can maybe use those to "get it".
Say we have these functions:
function wrap(...items) {
return items.map(item => ({ value: item }))
}
function wrapArray(items) {
return items.map(item => ({ value: item }))
}
function unwrap(...items) {
return items.map(item => item.value)
}
function unwrapArray(items) {
return items.map(item => item.value)
}
How can I type these so that for example the following would go work type-wise?
const items = [{ value: 4 }, { value: 'foo' }]
const [num, str] = unwrap(...items)
console.log(num.toFixed(2))
console.log(str.charAt(0))
Here's a playground with the functions, and a "test" for each. They're using non-variadic types, which of course doesn't quite work, and I don't understand how to write them so that they do work:
TypeScript Playground