type Foo = {
(trx: Transaction | undefined, event: SomeEnum.OPT_1, params: { whatever: number }): Promise<void>;
(trx: Transaction | undefined, event: SomeEnum.OPT_2, params: { whatever: string }): Promise<void>;
};
export const foo: Foo = async (trx: Transaction | undefined, event: SomeEnum, params: object | undefined) => {};
I'm experimenting with function overloading, the above code works but in my real world case I will have something like 40 overloads where only some of the parameters will be different, so I was wondering, is there a way to improve the code so I don't have to replicate all the invariant elements over and over? In the example, the first parameter trx
will always be the same, also the return type of the function will always be the same. I tried somethig like this but typescript clearly doesn't like it:
type FooPartial<T1 extends SomeEnum, T2> = (trx: Transaction | undefined, event: T1, params: T2) => Promise<void>;
type Foo {
FooPartial<SomeEnum.OPT_1, { whatever: number }>
FooPartial<SomeEnum.OPT_2, { whatever: string }>
}