#Variadic Tuple Types: Does this `type Arr = readonly any[];` provide any functionality?

11 messages · Page 1 of 1 (latest)

alpine oysterBOT
#
bawdyinkslinger#0

Preview:```ts
type Arr = readonly any[]

function concat<T extends Arr, U extends Arr>(
arr1: T,
arr2: U
): [...T, ...U] {
return [...arr1, ...arr2]
}

const a = concat(
[1, 2, 3] as const,
["a", "b", "c"] as const
)
// ^?```

weary robin
#

This seems to work just like this example:

alpine oysterBOT
#
bawdyinkslinger#0

Preview:```ts
function concat<
T extends readonly any[],
U extends readonly any[]

(arr1: T, arr2: U): [...T, ...U] {
return [...arr1, ...arr2]
}

const a = concat(
[1, 2, 3] as const,
["a", "b", "c"] as const
)
// ^?```

weary robin
#

But it's always hard for me to tell if I've modified behavior in these situations.

sour burrow
#

Personally I'd instead use:

concat<const T extends unknown[], const U extends unknown[]>

And with that you don't need the as const anymore.

weary robin
#

I can't remember if I already learned that

#

I think I did

weary robin
sour burrow
#

Your first two examples are indeed equivalent

#

type A = B is called a type alias after all, A is just a way for you to write B.

weary robin
#

Okay thanks