Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript merge tuples

How to merge tuples together? I need to add one tuple after end of another like below.

type MergeTuple<A extends any[], B extends any[]> = [...A, ...B];

Only thing that works is this:

type MergeTuple<A extends any[], B extends any[]> = [A[0], B[0]];

But that works with static count of items

MergeTuple<['a'], ['b']> // is ['a', 'b']

Not working for this

MergeTuple<['a', 'b'], ['c']> // is ['a', 'c'] expected ['a', 'b', 'c']
like image 392
luky Avatar asked Jun 17 '26 17:06

luky


1 Answers

Concatenating Tuples in TS is currently just possible with very ugly workarounds.

Here is how one library does it

You don't want to do that yourself. Ask yourself if you really need that, and if the answer is yes, do yourself the favor and use a type library like typescript-tuple.

like image 166
phry Avatar answered Jun 20 '26 13:06

phry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!