With typescript 4 now released, I was hoping things would be easier but I still haven't figured out how to achieve this.
My goal is to create a function that accepts a tuple containing a specific Generic and returns a Generic containing the values.
interface STORE<T> {
data: T;
}
const merge = <U extends any, T extends readonly STORE<U>[]>(values: T): STORE<U[]> =>
values.reduce<STORE<U[]>>(
(acc, item) => {
return { data: [...acc.data, item.data] };
},
{ data: [] },
);
For instance, executing this function should yield the following result:
assert(merge([{ data: 'test' }, { data: 2 }]), { data: ['test', 2]});
Unfortunately, this currently outputs the value as STORE<unknown[]>
instead of preserving the type from the input.