If I have an object with functions that return numbers:
const obj = {
foo() { return 1; }
bar() { return 2; }
baz(num: number) { return num; }
}
The expected output of typeof obj
would be:
{
foo: () => number;
bar: () => number;
baz: (num: number) => number;
}
Now, I need to create a type filter that takes the above typeof
and returns a similar object type, but with all functions now returning void
:
// Using object type to filter
Filter<typeof obj>
// Result
{
foo: () => void;
bar: () => void;
baz: (num: number) => void;
}
I have created a filter that successfully converts the function return types to void
:
export type Filter<O extends { [key: string]: (...args: any[]) => number }> = {
[K in keyof O]: (...args: any[]) => void;
}
The issue I am facing is passing down argument generics to the new functions. Currently, the arguments are defined as any[] which lacks type checking:
// No error shown but not ideal
{
foo: (...args: any[]) => void;
bar: (...args: any[]) => void;
baz: (...args: any[]) => void;
}
How can I propagate the argument generics to the newly defined functions? Any assistance on this matter would be greatly appreciated! 😄😄😄