#Keep array as a tuple

13 messages · Page 1 of 1 (latest)

idle falcon
#

I have the following function that takes in an array of strings. When I call the function and pass in an array of known values (aka a tuple), it makes the type an array of the types of the values in the array, not a tuple. How do I make it use a tuple?

royal wraithBOT
#
space.yg#0

Preview:```ts
function f<Cols extends any[]>(cols: Cols) {}

f(["hi", "hello"])
// Cols is string[], not ["hi", "hello"]```

idle falcon
#

Here is why I need it, I have this other type that takes in an object and an array of keys of the object and returns an array (with the same order as the keys) of the types of each key. It works when I use it as a type and I pass in a tuple, but when I use it in a function, the function converts the type to an array of all of the types in the array.

agile smelt
#
function f<const Cols extends any[]>(cols: Cols) {
idle falcon
idle falcon
agile smelt
#

You may want to do extends readonly any[] so it can also accept as const tuples

#

Not sure what the JSDoc equivalent would be, if there is one

#

Looks like you'd have to declare the entire function's entire type

idle falcon
#

If it works, it works

#

Thanks for the help!