Imagine having a function that takes in a dataset which is an array of (identically-typed) tuples:
type UnknownTuple = any[]
function modifyDataStructure<T extends UnknownTuple>(list: T[]) {
...
}
The goal is to define a second argument with the same length as the tuples, but with different types (for example, string labels):
const entries = [
[7, 'Moo', true],
[3, 'Sophie', false],
[4, 'Tip', true]
]
modifyDataStructure(entries, ['age', 'name', 'is_fuzzy']) // success
modifyDataStructure(entries, ['age', 'name']) // error
How can we specify the type for the second argument?
type UnknownTuple = any[]
function modifyDataStructure<T extends UnknownTuple>(list: T[], labels: ???) {
...
}
This question post "Typescript length of a generic type" is quite similar, but involves varargs and higher-order functions, making it more complex. Here, I aim to simplify and focus on clarifying this scenario.