Can a type-aware declaration be written in Typescript for a function that takes a tuple and returns a new one with an appended item, without using function overload?
In short, I need a function that performs the following:
[T1, T2, ... Tn] + U => [T1, T2, ... Tn, U]
One obvious way to achieve this is through multiple overloads:
function append<A, B>(a: [A], b: B): [A, B];
function append<A, B, C>(a: [A, B], c: C): [A, B, C];
function append<A, B, C, D>(a: [A, B, C], d: D): [A, B, C, D];
function append(tuple: any[], b: any): any[] {
return tuple.concat([b]);
}
Is it possible to write this in the form of:
function append<T extends any[], U>(t: T, u: U): ??? => ???;