My goal is to create two distinct overloads for event emission: one that corresponds to custom events, with specific listener arguments and dispatch time arguments
export type EngineEvents
= ['window-exit', () => void]
| ['test', (code: number) => void]
| ['pre-init', () => void]
| ['post-init', () => void]
| ['tick-start', () => void]
| ['draw-start', () => void]
;
The challenge lies in mapping these types to the dispatch type without repetition. I've attempted the following:
export type EventArgs = [EngineEvents[0], ...Parameters<EngineEvents[1]>];
However, this results in a tuple containing a union in each cell, which is not ideal. Ideally, I would like the tuple to be mapped the other way around, as shown below:
// Instead of
type A = [ 'a' | 'b', 1 | 2 ];
// Have this:
type B = [ 'a', 1 ] | [ 'b', 2 ];
I have tried using the T extends any? whatever : never
method suggested in this answer: TypeScript: Map union type to another union type
Unfortunately, it did not yield the desired result.
export type ArgumentExpand<U> = U extends any[]? [U[0], ...U[1]] : never;
Is there a way to individually map each element of the union so that when accessing the first and second elements of the tuple, they are not mixed up?
Essentially, looking for a mapping operation for types.