typescripttypescript-genericstypescript3.0

TypeScript infer second argument of a function first argument


Is it possible to infer a function argument from another argument? I want to infer 2nd argument based on inference of 1st argument.

Current code can be on GitHub slickEventHandler.interface.ts and is the following

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>;
}

and this allows me to do the following (live code here)

const onSelectedRowsChangedHandler = this._grid.onSelectedRowsChanged;
(this._eventHandler as SlickEventHandler<GetSlickEventType<typeof onSelectedRowsChangedHandler>>).subscribe(onSelectedRowsChangedHandler, (_e, args) => {
  args.column;
});

and this does proper inference

enter image description here

However, what I'm looking for is a much simpler approach as shown below (but what I currently get using the code below is args being infered as any)

this._eventHandler.subscribe(onHeaderCellRenderedHandler, (_e, args) => {
  const column = args.column;
  const node = args.node;
});

So as I mentioned in the question, for that to work, I would need to infer first argument and somehow make an alias that can be used by the second argument. Is that possible with latest version of TypeScript?


Solution

  • I think you want the methods to be generic not the interface, so you would put the generic at the function definition not the interface name.

    export interface SlickEventHandler {
      subscribe: <T>(slickEvent: SlickEvent<T>, handler: Handler<T>) => this;
      unsubscribe: <T>(slickEvent: SlickEvent<T>, handler: Handler<T>) => this;
      unsubscribeAll: () => this;
    }
    

    then the generic will be inferred when you call the method.