I am currently working on converting a function to TypeScript that involves an Array of mixed type tuples, similar to Promise.all
. I am struggling to set up the definitions for this particular function.
export type Type<T> = [T, boolean];
function fn<T1, T2>(list: [Type<T1>, Type<T2>]): [T1, T2] {
return list.map(([value]) => value);
}
fn([
['string', true],
[123, false],
// More items
]);
// Output: [ 'string', 123 ]
The Promise.all
method has different options such as:
all<T1, T2, T3>(values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>]): Promise<[T1, T2, T3]>;
all<T1, T2>(values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>]): Promise<[T1, T2]>;
all<T>(values: readonly (T | PromiseLike<T>)[]): Promise<T[]>;
I am wondering if there is a more efficient way in TypeScript to handle arrays without explicitly defining all possible lengths?