Can a function argument be inferred from another argument? I'm interested in inferring the 2nd argument based on the inference of the 1st argument.
The current code can be found on GitHub at slickEventHandler.interface.ts and appears as follows:
export type GetSlickEventType<T> =
T extends (infer U)[] ? U :
T extends (...args: any[]) => infer U ? U :
T extends SlickEvent<infer U> ? U : T;
export type GetEventType<T> = T extends infer U ? U : T;
type Handler<H> = (e: SlickEventData, data: GetSlickEventType<H>) => void;
export interface SlickEventHandler<T = any> {
subscribe: (slickEvent: SlickEvent<T>, handler: Handler<T>) => SlickEventHandler<T>;
unsubscribe: (slickEvent: SlickEvent<T>, handler: Handler<T>) => SlickEventHandler<T>;
unsubscribeAll: () => SlickEventHandler<T>;
}
This setup allows me to do the following (see live code here):
const onSelectedRowsChangedHandler = this._grid.onSelectedRowsChanged;
(this._eventHandler as SlickEventHandler<GetSlickEventType<typeof onSelectedRowsChangedHandler>>).subscribe(onSelectedRowsChangedHandler, (_e, args) => {
args.column;
});
And this implementation provides correct inference.
https://i.sstatic.net/m9tUE.png
However, I am seeking a simpler approach as shown below (though currently using the code below results in args
being inferred as any
):
this._eventHandler.subscribe(onHeaderCellRenderedHandler, (_e, args) => {
const column = args.column;
const node = args.node;
});
Therefore, to achieve that, I would need to infer the first argument and create some kind of alias that can be utilized by the second argument. Is this achievable with the latest version of TypeScript?